Everything I learned about shell scripts
Many applications do not require coding. Many problems can be solved with unix programs and pipelines. Python is good for glue, and so are shell scripts. In fact, shell scripts may even be simpler.
In the past few years, I have used shell scripts extensively, either for my own tools, for CI/CD pipelines, or for my applications. In this post, I want to store that knowledge, and I hope it will be useful for other computer programmers and engineers.
The HashBang
This is a sample hash-bang: #!/bin/zsh
(or more commonly: #!/bin/bash
or #!/bin/env bash
). You start an executable script with a hash-bang. The hash-bang defines the environment or application that will execute your code. You put it at the top of the script. You may put comments above the hash-bang.
# script.sh
# Author: David Kuda
# Date: January 2023
#!/bin/zsh
echo "Hello World"
This script will be executed in the environment defined in the hash-bang. This means that zsh
will execute echo “hello world”
The hash-bang is also known as “sh-bang”.
You can also execute python files, even from virtual environments or from conda:
# greet.py
#!/usr/local/Caskroom/miniconda/base/bin/python3
import sys
name = sys.argv[1]
print(f"hello {name}")
# to execute the python script:
chmod 700 greet.py
./greet.py David
# prints: hello David
chmod
Once you have a file script that you would like to execute (say with a double click, or in your terminal with ./script.sh
, you first need to change the file permissions. chmod
changes these file permissions.
# change file permissions:
chmod {permissions} {file_path}
# examples:
chmod 744 script.sh
chmod 400 sshkey
There are three entities in a unix environment:
- First Digit: You, the user
- Second Digit: The group, you belong to
- Third Digit: All others
chmod takes three digits. The first digit is for the user, the second for the group, and the third for all others.
chmod 744 script.sh
# 7 for the user: allow read, write, and execute
# 4 for the group and all others: allow read-only
chmod 400 sshkey
# 4 for the user: allow read-only
# 0 for the group and others: allow nothing (no read,
# no write, no execute)
Many tutorials or stackoverflow posts will advise to use chmod +x {file}
, but I think that’s a bad idea. +x
translates to 777
, which means that you, your group and everyone else gets the permission to execute your file. 744
or 700
is safer!
- 0: none
- 1: execute only
- 2: write only
- 3: write and execute
- 4: read only
- 5: read and execute
- 6: read and write
- 7: read, write and execute
The First Column of ls -l
ls -l
prints the permissions of all files and directories:
> ls -l
drwxr-xr-x david staff 192 B Tue Jan 24 13:43:39 2023 blog
.rw-r--r-- david staff 67 KB Tue Dec 13 14:25:00 2022 CV David Kuda.pdf
.rw-r--r-- david staff 1.1 MB Sun Jan 8 22:59:51 2023 Hoare78.pdf
.rw-r--r-- david staff 280 B Fri Jan 20 18:34:17 2023 main.go
.rwx------ david staff 33 B Tue Jan 24 22:17:30 2023 script.sh
# you can split the first col into four groups:
| (1) d | (2) rwx | (3) r-x | (4) r-x |
# 1: d represents a directory, . represents a file
# 2: permissions for the user
# 3: permissions for the group
# 4: permissions for all others