CodingNic

Advanced Terminal Commands

Shell Scripting and Vim

Advanced Terminal Commands 18 min read

Shell Scripting and Vim

Objectives

By the end of this chapter, you should be able to:

  • Write a simple shell script that accepts arguments
  • Open and edit files in the terminal using Vim

๐Ÿ’ก Why this matters: Every command so far has been typed fresh, one at a time. A shell script turns a whole sequence of commands into a single, reusable one โ€” and Vim means you can edit those scripts (or any file) without ever leaving Terminal.

Your First Script

Terminal commands so far have all been one-off โ€” retype the whole thing if a filename changes. A shell script fixes that: a file full of commands you can run as a single unit. Create first.sh containing:

bash
echo "Hello World"

Run ./first.sh and you’ll get permission denied โ€” a script needs execute permission before it can run as a program:

bash
chmod 755 first.sh
./first.sh

That should print Hello World.

Arguments

Scripts get more useful once they can accept input. Create second.sh:

bash
echo Hello $1

$1 refers to the first argument passed to the script. Make it executable and try it two ways:

bash
chmod 755 second.sh
./second.sh
./second.sh World

Run with nothing, it prints just “Hello”. Run with World, it prints “Hello World” โ€” $1 picked up the argument. $2, $3, and so on work the same way for additional arguments.

Try It: Write sum.sh

Write a script called sum.sh that accepts two numbers and echoes their sum. You may need to look up how arithmetic works inside a shell script โ€” one working solution:

bash
echo $(($1+$2))

A More Practical Script

You’ve already typed ps aux | grep <name> to hunt down a process. Wrapping it in a script means you never retype the pipe again:

bash
ps aux | grep $1

Save that as process.sh, chmod it executable, and ./process.sh less searches for any process with “less” in it. If you use a script like this often, put it somewhere on your $PATH (you covered PATH two lessons ago) so you can run it from anywhere without the ./.

Vim

Vim is a terminal-based text editor โ€” intimidating for the first few minutes, and genuinely fast once it clicks. Open any file with vi filename.

Vim opens in normal mode, meant for navigating, not typing โ€” if you type letters and nothing appears, that’s expected. Press i to switch to insert mode, where typing works as you’d expect anywhere else. Press Escape to go back to normal mode before running any command.

Want to Do this
Start typing i (enters insert mode)
Stop typing, back to normal mode Esc
Quit without saving Esc then :q (or :q! to force it)
Save and quit Esc then :wq, or Shift+Z+Z

For guided, hands-on practice beyond this lesson, run vimtutor in your terminal โ€” it’s a built-in interactive tutorial.

Try It

Scripts and file edits happen on your real filesystem, so work through these yourself:

  1. Write and run first.sh and second.sh as shown above.
  2. Write sum.sh before looking at the solution.
  3. Open any file with vi, enter insert mode, add a line of text, then save and quit.
  4. Run vimtutor and get through at least the first lesson.

Recap

  • A shell script is a file of commands you can run as one unit โ€” make it executable with chmod 755 first.
  • $1, $2, etc. inside a script refer to the arguments it was called with.
  • Vim opens in normal mode for navigation; i switches to insert mode for typing, Esc switches back, and :wq saves and quits.

That’s the full course โ€” from finding your way around a directory to writing your own scripts and editing files without a GUI. The exercises in the next lesson pull all four modules together.