Master Bash Scripting: Essential Basics, Quirks, and Safety Tips
This article walks through Bash scripting fundamentals—including variable assignment, quoting, loops, conditionals, functions, return codes, and background jobs—while highlighting common pitfalls and safety practices such as always quoting variables and using the shellcheck linter.
Yesterday I realized that even after ten years of using Bash, there are still basic details I don’t fully understand, so I wrote this article.
Author: oschina Source: https://www.oschina.net/translate/bash-scripting-quirks-safety-tips
We will cover:
Some Bash basics (e.g., how to write a for loop)
Miscellaneous quirks (e.g., always quote your Bash variables)
Bash script safety tips (e.g., always use set -u )
If you write shell scripts, you should know about the linter shellcheck to improve your scripts.
Variable Assignment
In Bash you assign variables without spaces: VARIABLE=2 Reference them with $VARIABLE. Do not put spaces around the = operator; otherwise you may unintentionally run a command named 2 and set the variable to an empty string.
Variables are usually uppercase, but not required. Most are strings; Bash also has array variables.
Referencing Variables with ${}
To avoid ambiguity when appending text, use ${MYVAR} instead of $MYVAR:
mv $MYVAR ${MYVAR}__bak # correctGlobal, Local, and Environment Variables
Bash has three kinds of variables. Environment variables are visible to all child processes: export MYVAR=2 Global variables are simple assignments: MYVAR=2 Local variables exist only inside functions and are rarely used in Bash scripts.
For Loop
A typical loop that prints numbers 1 to 10:
for i in `seq 1 10`; do
echo "$i"
doneYou can write it in one line: for i in `seq 1 10`; do echo $i; done You can also iterate over command output, e.g., using seq, or use $(command) syntax.
If Statement
Bash if statements require spaces around brackets. Both [ ] and [[ ]] work, but [[ ]] is preferred:
if [[ "vulture" = "panda" ]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fiYou can test file existence with [[ -e /tmp/awesome.txt ]] or the test command.
Functions Are Simple
Define and call a function without parameters:
my_function () {
echo "This is a function"
}
my_functionAlways Quote Your Variables
Never use an unquoted variable in tests:
X="i am awesome"
Y="i are awesome"
if [ "$X" = "$Y" ]; then
echo awesome
fiReturn Codes, &&, and ||
Every Unix program returns a code from 0 (success) to 127 (failure). You can chain commands based on success: create_user && make_home_directory Or run the second command only on failure:
create_user || make_home_directoryBackground Processes
Start a job in the background with & and bring it to the foreground with fg. List jobs with jobs. Use wait to wait for all background jobs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
