Many people write bash scripts or vibe code them now which to some extend is fine. However bash is very tricky and different. It’s even trickier if you are coming from another more ‘pure’ programming language.
Let’s stick with one simple example.
Our Python setup.
import sysdef very_destructive_action(msg): print(msg)filename = sys.argv[1]with open(filename, "r") as f: content = f.readlines() print(content)very_destructive_action("System down")
Now what would happen if I pass an argument that is corresponding to a non-existing file ? We could all bet that the program will stop with an error when it tries to evaluate the below line.
with open(filename, "r") as f:
In reality this is exactly what happens.
python3 setup.py gfile Traceback (most recent call last): File "setup.py", line 10, in <module> with open(filename, "r") as f: ~~~~^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'gfile'
Perfect, so far the world around is set up based on our logic and expectation. We could now even add some exception handling or call some native python features before opening the file to make sure it’s there and move on.
Now let’s try the same thing, but in bash.
very_destructive_action() { echo "$1"}cat $1very_destructive_action "System down"
Now the coin has 2 sides:
- We would expect the same thing to happen that happened in python, which is the program stops.
- You know this whole example workflow is not random or you are fluent in bash :)
Anyway, let’s see the result.
bash setup.sh gfilecat: gfile: No such file or directory (os error 2)System down
This is the most important feature in bash in my humble opinion. If you do not know about it you would assume that in bash you might just write your logic the same way you do in some other programming language and if it fails on a particular line the script will stop there, but that’s not true.
There are a few ways how to handle this and some of them are pretty opinionated, but for the most part people would use set -e in the beginning of the script. Of course in bash even this solution has some limitation and it does not work in an until or while loops, if-tests, and list constructs.
Let’s try it out.
set -e very_destructive_action() { echo "$1"}cat $1very_destructive_action "System down"
bash setup.sh gfilecat: gfile: No such file or directory (os error 2)



Leave a comment