In a world filled with high-level programming languages like Python, JavaScript, and Ruby, why should one dip their toes into the arcane art of Bash scripting? Undoubtedly, Bash scripting might not be the most elegant or modern way to accomplish something programmatically, but, it provides incredible power and efficiency when working with Linux based systems. With this tutorial, we will journey beyond the mundane and into the territory of advanced scripting in Bash.
Let’s start our expedition with conditional statements and loops. Most of us are familiar with elementary if, else
, and while
loops, but do you know about conditional execution? Bash offers operators &&
and ||
for AND and OR operations. They are different from classic control flow statements, as in they can execute commands pertaining to the outcome of a previous command.
# Using '&&'
[ -d "mydirectory" ] && echo "Directory exists"
# Using '||'
[ -d "mydirectory" ] || mkdir mydirectory
In the first line, echo "Directory exists"
will only be executed if “mydirectory” indeed exists, thanks to &&
. Similarly, mkdir mydirectory
will execute if “mydirectory” does not exist, courtesy of ||
.
Scripting is about automation, and automation is about convenience. Bash provides ways to receive inputs from a user, thus creating interactive scripts. You can use the read
command for this.
echo "Enter your name:"
read name
echo "Hello $name"
Here, whatever input the user provides after “Enter your name:” prompt will be stored in name
variable. You can even add -p
flag to read
, allowing you to specify the prompt string within the command itself.
Moving on, let’s discuss debugging your Bash scripts. Debugging can be quite tricky in Bash. One reliable method is to use set -x
before the portion of the script you want to debug and set +x
after.
Further, we can explore process substitution. It’s a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in place with the name of the file.
# Compare two sorted files
diff <(sort file1) <(sort file2)
According to Richard Blum, author of “Linux Command Line and Shell Scripting Bible”, process substitution “lets you use the output of a command anywhere a filename can be used.”
Lastly, we should bring attention to script portability. Creating scripts that can be used across different environments, distributions, or even different shell interpreters is a skill worth mastering. Ensuring you use the shebang line (#!) correctly, avoiding system-specific commands where possible, and being mindful of different versions of commands on different systems can go a long way in writing portable scripts.
However, Bash scripting is not devoid of pitfalls and ‘gotchas’. Here are a few examples:
Use double quotes with variables: Always enclose your variables in double quotes. “$var” is not the same as $var, especially when it contains spaces or other special characters.
Remember, blank variables are not null! Be cautious when testing variables. [-z “$var”] will return true even if $var is unset.
Floating point operation is missing in Bash. It only supports integer mathematics, so you may need to use external tools like
bc
for floating-point arithmetic.Keep in mind that local variables aren’t too local. They’re local to the function, and all the functions it calls - but not to a subshell.
In conclusion, mastering advanced Bash scripting is a journey worth taking. It might seem daunting at first, but the pay-off, in terms of knowledge and skills acquired, is considerable. Embrace the Bash!
For more examples and clarifications, you can go through various public repositories available on GitHub, like awesome-bash. Stand on the shoulders of giants to reach greater heights!