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:
echo "Hello World"
Run ./first.sh and you’ll get permission denied โ a script needs execute permission before it can run as a program:
chmod 755 first.sh
./first.sh
That should print Hello World.
Arguments
Scripts get more useful once they can accept input. Create second.sh:
echo Hello $1
$1 refers to the first argument passed to the script. Make it executable and try it two ways:
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:
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:
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:
- Write and run
first.shandsecond.shas shown above. - Write
sum.shbefore looking at the solution. - Open any file with
vi, enter insert mode, add a line of text, then save and quit. - Run
vimtutorand 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 755first. $1,$2, etc. inside a script refer to the arguments it was called with.- Vim opens in normal mode for navigation;
iswitches to insert mode for typing,Escswitches back, and:wqsaves 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.