3 Bash scripting techniques every Linux user should know

3 Bash scripting techniques every Linux user should know

Published Nov 17, 2026, 9:30 AM EST Graeme Peacock is a seasoned Linux expert with more than 15 years of hands-on experience. He has worked extensively with Ubuntu, Gentoo, Arch Linux, Qubes, and Fedora, gaining deep proficiency in everything from routine terminal operations to highly customized system builds. Graeme began his journey with Ubuntu, quickly mastering the command line and essential system administration skills. A year later, he moved to Arch Linux, where he spent nearly a decade refining his expertise through the installation and configuration of multiple minimalist systems. After some time, he moved to Gentoo, where he configured and compiled both server and desktop environments using normal and hardened profiles and frequently compiled custom kernels. Graeme moved to Qubes in 2016, where he has remained ever since. Graeme has extensive experience with highly configurable tools such as Vim, Neovim, and Emacs, and he maintains his own complex configurations. He is also highly proficient with Bash, Zsh, and dozens of utilities. Graeme holds a B.S. in software engineering and has a strong passion for programming and web development. He is proficient in Golang, Python, Bash, JavaScript, TypeScript, HTML, and CSS. He also has considerable experience with Docker and is currently working on learning Kubernetes. Have you been thinking about biting the bullet and learning Bash scripting? Or perhaps you're like me, and you're always looking for ways to improve. I have three tips that will improve every Bash script you write. Passing arguments, writing a help menu, and debugging can be burdensome. However, the getopts command, Heredoc, and the "set -x" option are common solutions that not everybody knows about. Let's tackle that problem and learn how these things can improve your Bash scripts. When you execute a script months after you wrote it, the first thing you will need is a help menu. Without that, you need to dig into the source code to jog your memory, which is an unnecessary hassle. When I started writing help menus in Bash, I used a function that ran dozens of echo statements. _help() { echo -e "Usage foo [OPTION]\n" echo -e "OPTIONS" echo -e " -h display this help menu" echo -e " -r do NOT press this button\n" echo -e " -a echo something\n" } Even though I aligned the menu elements to make them more readable, a Heredoc is far superior: _help() { cat <<EOF Usage: $(basename "$0") [OPTIONS] OPTIONS -h display this help menu -r do NOT press this button -a echo something EOF } The code is tidier, more readable, easier to write, and without special characters (e.g., "\n") to format it. To make your life easier, you should prefer a Heredoc over separate echo statements. Also, notice the use of "$(basename $0)," which extracts the filename from the script path so that our help menu displays the name of the script. "$0" is the script path. "basename" extracts the filename from a path. Parse command line arguments like a pro Now that we have a menu, we need to process the arguments. The most common and easiest way is to access the positional parameters provided by Bash. #!/usr/bin/env bash echo "The first parameter: $1" echo "The second parameter: $2" However, that approach becomes complex when handling options (aka flags) and values. The getopts command is the go-to solution, and it's built into Bash. Below, I have expanded the earlier help menu example to allow us to process options: #!/usr/bin/env bash _help() { cat <<EOF Usage: $(basename "$0") [OPTIONS] OPTIONS -h display this help menu -r do NOT press this button -a echo something EOF } while getopts "ra:h" opt; do case "$opt" in r) curl 'https://ascii.live/rick' ;; a) echo "ECHO -$opt: $OPTARG" # For example, "ECHO -a: foo" ;; h) _help exit 0 ;; esac done Accepting basic options The basic form of the getopts command is: getopts OPTSTRING OPTVAR The options string ("optstring") is how we specify available options. If we want to accept the -a, -b, and -c options, then we specify the option string as follows: getopts "abc" OPTVAR There are three facts we can use to our advantage: The option variable ("optvar") stores the option name currently being processed (e.g., a, b, or c). When complete, getopts returns a number greater than zero. A while loop will continue until it receives a falsy value, like an exit code of 1. Simply put, the following code will process each option: # This code is incomplete and does not work. while getopts "abc" opt; do ... done Because the "opt" variable stores the option name (e.g., a, b, c), we can use a case statement to help process them: # This code is incomplete and does not work. while getopts "abc" opt; do case "$opt" in a) ... ;; b) ... ;; c) ... ;; esac done The "case $opt in" statement uses the "opt" variable and then matches it against "a)", "b)", or "c)"—the first match wins. Case statements use pattern matching, and we could do a lot more with them if we wanted to. Accepting options with values Getting an option value is the last thing to learn—for example: "-a foo." First, we must tell getopts that an option accepts a value—add a colon after it in the option string. For example, "a:" tells getopts that "-a" accepts a value. It then becomes accessible via the "$OPTARG" variable. # This code is incomplete and does not work. while getopts "a:b" opt; do case "$opt" in a) echo "$OPTARG" ;; b) ... ;; esac done Set a flag for optional debugging Bugs are inevitable. Fortunately, Bash has a simple way to debug your script by printing all statements when they execute. We can also enable it optionally. Let's change our script to make that happen, using a "set -x" statement. #!/usr/bin/env bash set -x # <-- ENABLE DEBUG STATEMENTS. _help() { cat <<EOF Usage: $(basename "$0") [OPTIONS] OPTIONS -h display this help menu -r do NOT press this button -a echo something EOF } while getopts "ra:h" opt; do case "$opt" in r) curl 'https://ascii.live/rick' ;; a) echo "ECHO -$opt: $OPTARG" # For example, "ECHO -a: foo" ;; h) _help exit 0 ;; esac done Now let's run the script: ./script.sh -a foo You can see from the image that Bash prints each command it executes and most variables it evaluates. You could enable it optionally via a flag: # This code is incomplete and does not work. while getopts "ra:hd" opt; do case $opt in d) set -x # <-- ENABLE DEBUG STATEMENTS. ;; # ... esac done Or you can enable it via an environment variable at the top of the script: #!/usr/bin/env bash [[ -n "$DEBUG" ]] && set -x # <-- ENABLE DEBUG STATEMENTS. Then enable debug mode like this: DEBUG=true ./script.sh -a foo Doing it that way means it will print the option processing code too. So that's it—a sensible help menu, CLI argument parsing, and debugging. Each of these will apply to nearly every script you write. In fact, they're so common, you could write a script to make other scripts, populating them with the concepts you've learned today. In programming, we call them snippets. I have a "create" and "edit" Bash script that does just that. Bash is a much larger topic, but these three simple tips should set a solid foundation for you.

Original Source

Read the full article at Howtogeek →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.