Home » Linux » Shell » Bash Scripting Tips

LinuxScrew’s Linux Shell/Bash Scripting Tips

Here are some handy tips to keep in mind when writing your shell scripts in Linux.

Shell scripts are a versatile way to automate your workflows in Linux (and MacOS, and now Windows, with the Windows Subsystem for Linux). Shell scripting syntax and behaviour does have its quirks, and there are useful shortcuts you can take as well to simplify your scripts.

Bash Scripting Tips

These tips are collected from around the internet, with a few of my own thrown in.

Bash scripts will keep running even if there’s an error!

This can cause problems, especially if accepting user input and working with files. To prevent this behaviour add the set command

set -eu

…to the beginning of your scripts. –e tells the script to exit on error, and –u tells the script to exit if a variable is not set.

ALWAYS include the shebang

Always include the shebang as the first line of your script:

#!/bin/sh

This tells the system which shell should be used to interpret the script, and is important for compatibility. A script that is written for Bash may behave differently when run within Zsh, but by including the shebang, you can ensure it is interpreted with the right shell.

Never user sudo in your script

This causes ambiguity if you run a script, and can cause issues when using pipes and redirection. It’s also a security problem. If a script needs root permissions, the whole script should be run as root or using sudo.

Check whether the script is being run as root/sudo in a script

Add this command to the beginning of your script to check that it is running with root/sudo privileges. This command will cause your script to fail before it tries to do anything, rather than when you try and run a command that requires root privileges

# Check if running as root
if ((EUID!=0 )); then
    printf "Please run as root"
exit 1
fi

Skip apt prompts with -y

Add -y to the end of your apt commands to skip the prompt and allow your scripts to run without user input:

sudo apt install nano -y

or

sudo apt upgrade -y

Quote your paths!!!

Quote your paths when writing Bash scripts. It’s important enough we have a whole article explaining why.

And correct for the current working directory

CURR_DIR="$(dirname $0);"
printf -- 'moving application to /opt/app.jar';
mv "${CURR_DIR}/application.jar" /opt/app.jar;

Set default values for variables from user input

When prompting for user input, default values can be set by using the following:

echo "Enter a value"
read val
val=${val:-"Default Value"}

If the variable val is empty after it has been read, the default value will be set.

Leave lots of comments

It’s for your future self’s sake – explain what you are doing, and why you are doing it. Being clear on the intention of your code is a good sanity check and aids with debugging, and extensive commenting makes it easy to return to your code at a later date.

Print, Print, Print

Print as much information to the console as you can, so that if your script crashes, you know where it crashed. Bash scripts by default are not necessarily verbose.

Emit an exit status

If your script encounters an error, make sure it exits with an appropriate exit code, so that other scripts which may call it can react appropriately.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment