Bash Scripting Tutorial for Beginners
Bash Shell Scripting Definition
Bash:Bourne-Again SHell
Bash is a command language interpreter.conclusion
Do not be afraid to break things as that is perfectly normal. Troubleshooting and fixing code is perhaps the best booster for you to enhance your understanding of bash scripting and to improve your ability.
syntax
exit code
the shell assigns its exit code to the $?
environment variable. The $?
variable is how we usually test whether a script has succeeded or not in its execution.
set -e
1 | Causes the script to exit immediately if any command fails (returns a non-zero status). |
set -x
1. Debugging: When you’re trying to troubleshoot a script, set -x helps you see exactly what commands are being executed and in what order.
2. Selective Debugging: You can use set -x and set +x around specific parts of your script to narrow down the problematic sections.
1 | !/bin/bash |
output:
1 | This is a normal message |
shift 2
- Removes the first two positional parameters from command line arguments
- Shifts remaining parameters left by 2 positions
- Useful when processing command line options/arguments in loops
{xx..xx}
1 | for node in node{1..9} node{20..29} ; do |
${args[@]}
The ${args[@]}
syntax in bash is used to expand an array variable named args
1 | !/bin/bash |
sigle quota and double quota
single quotes '...'
prevent the shell from expanding ${G_FSNAME}
. As a result, the literal string ${G_FSNAME}
is inserted instead of the value of the environment variable.
Use Double Quotes for Variable Expansion, to allow the shell to expand the variable
point
(cd third-party && ...)
这样的语法,这是一个命令替换,它实质上是先临时改变到third-party
目录执行里面的命令,然后脚本会继续在原先的目录中执行。
nohup
output of a script to a specified file
1
nohup your_script.sh > output.log 2>&1 &
0: Standard input (stdin)
1: Standard output (stdout)
2: Standard error (stderr)
>&: This is used to redirect the output.
&: Puts the command in the background.
getopt
1 | local long_opts="role:,args:,help" |
This snippet is a common pattern in Bash scripting to parse command-line options using getopt. Here’s a line-by-line breakdown:
Explanation:
local long_opts="role:,args:,help"
Defines long options supported by the script:
- role: means –role requires a value (e.g., –role=admin)
- args: means –args requires a value
- help means –help is a flag with no value
: means the option requires an argument; no colon means it’s a flag.
local args=getopt -o ra –long $long_opts -n “$0” – “$@”
- -o ra: short options -r and -a are accepted
- –long $long_opts: specify long-form options (see above)
- -n “$0”: used for error messages (e.g., script name)
- – “$@”: separates option parsing from positional arguments
This parses the options passed to the script and stores the normalized result in args.
1 | Example: ./myscript.sh --role=admin --args=val --help foo bar |
eval set -- "${args}"
This re-sets the $1, $2, … positional parameters to the parsed list.
So later in the script, you can safely loop through options using:
1 | while true; do |
Summary
Line | Purpose |
---|---|
long_opts=… | Defines supported long options |
getopt … | Parses CLI arguments |
eval set – … | Makes parsed options available for normal while-case parsing |