Function call stack and backtraces

Enabling tracing with set -x is a good way to debug, but sometimes that’s either not enough or too much. There are situations you need to display the execution call stack at some point in a script in the form of a backtrace. This kind of functionality was a real saviour when I was debugging scripts that were too silent (having both output descriptors redirected to /dev/null). Functions and libraries The truth is that the longer the script, the more you need to break it into separate functions. The more functions you have, the more you need to split them among different library files, toRead More →

Positional parameters: changing and handling them

One of the best features in bash programming is the ability to change and play with the positional parameters. Why? Many times when I told someone about this “secret”, they hit me with the big question of Why? Why would one want to do change the positional parameters of a function or a script? Here’s my take on this. When you change the positional parameters, your variables are shorter ($1, $2, etc). You also benefit from using the shift builtin and you’re able to use getopts easier. All these capabilities can be translated to using arrays, but there’s nothing like the universal understanding of whatRead More →

Bash redirections in broader contexts

Everybody uses redirections. Whether it is to or from /dev/null, or a file, or even a command. But every so often I see sequential commands redirecting their output to the same place. A proof that very few grasp that you can use bash redirections in broader contexts, like compound commands, groups or blocks of code. Brace list Take the following example: command1 2>/dev/null command2 command3 2>/dev/null There is, indeed, the case where we need the stderr information from command2, but in real life, I’ve found that quite rare. For all the other cases, when we don’t need the stderr of command2, we can group allRead More →

Filtering content with bash instead of grep

I get it, grep is an advanced content filtering tool. I’m not “bashing” its use. I do it on a daily basis, too. However, most of grep calls I’ve seen are for simple words or sometimes simple patterns. And that might come easier while you’re typing at the bash prompt, but might be too “expensive” to do it inside a script. The problem with using grep in these simple situations is that you’re dragging a whole lot of executable code. This code is most of the times executed in a subshell. That’s because you’re either using a pipe or using a command substitution, each ofRead More →

In my experience, most of the code I’ve seen redirects output of stdout or stderr to /dev/null. Not only bash supports a lot more redirection potential than those, but correct input redirection seems to have been replaced by overused constructs, which are often slower. Lose the cat I know that when you write a command at the interactive shell prompt, before using grep, you might want to peek inside the file, so you first do a cat file and then when you know what to filter, you press the up arrow on your keyboard and add | grep pattern at the end of the catRead More →

I’m here to tell you the naked truth. Not only bash knows how to do everything seq does, but it also does it better. There, I said it. I know, your older brother or your mentor taught you to write for i in `seq 5` but using pure bash is so much better. Curious how? Read on! History Back in the 80s, when the shells were featureless, there was a strong case for seq. Coders needed a way to iterate through a sequence of numbers in their shell scripts. It was so early in the computers era that Perl didn’t even exist as a fallbackRead More →

Proper hanlding of return/exit codes

I often see coders being confused about how to use the return/exit code of a command. There isn’t a consistent message about when $? should be used or whose command it belongs to in the first place. Conditionals vs. $? First of all, it needs to be clear that the if and while constructs consider the exit code of the conditional command and execute one branch or another accordingly. This means that the following piece of code: if command; then true dat else no way fi will first execute command and wait for its completion. If its exit code is 0, then it will nextRead More →

Shells, subshells and PIDs

Sometimes you need to find out the PID of your running shell. But when your current code runs in a subshell instance, you might get confused about the difference between the $BASHPID and $$ variables. One note here, the BASHPID variable has been introduced in bash version 4.0, so it’s not present in any versions that are older than that. Notably, the one that ships with Mac OS X, is bash 3.2. Uh, subshells? First of all, you might wonder, what is a subshell and how it differs from a… shell? Well, a full-blown shell is one when bash is executed and initialized. So wheneverRead More →

To quote, or not to quote, that is the question

When was the last time you quoted a bash string just because you were not sure about if the characters that are contained are treated specially by bash? The worst part is that this commonly happens even when defining constants. I know about security, I know that having strings from end-users require special attention so we don’t end up executing code we don’t intend to. But these aren’t the cases I’m referring to. Two major situations where quotes are most probably not needed are: variable assignment to a constant value echoing simple strings Assigning a constant When you assign a constant string to a variableRead More →

Commenting code blocks in bash

Do you know how controversial the block commenting problem is in the Python world? Well, in bash this is simpler to address, yet still controversial – purists, you know who you are 🙂 I know, most editors support prepending the hash sign to every line of a selected block. But from some reason, I find this quite high maintenance. Let me explain why. For example, when I’m testing some patch, I want to disable an entire code block from being executed. If I prepend all lines with hashes I will generate a diff as big as the code block. If that code block is 3Read More →