shell bash

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.

Bash scripting Tutorial

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
2
3
4
5
6
7
8
9
10
#!/bin/bash

echo "This is a normal message"

set -x # Start debugging from this point
name="John"
echo "Hello, $name"

set +x # Stop debugging after this point
echo "Debugging mode is now off"

output:

1
2
3
4
5
This is a normal message
+ name=John
+ echo 'Hello, John'
Hello, John
Debugging mode is now off

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
2
3
4
5
for node in node{1..9} node{20..29} ; do
echo -n "$node: "
kubectl get node "$node" -o jsonpath='{.spec.taints}' || echo "Not found"
echo
done

${args[@]}

The ${args[@]} syntax in bash is used to expand an array variable named args

1
2
3
4
5
6
7
8
#!/bin/bash

# Define an array
args=("hello" "world" "test")

# ${args[@]} expands to all elements
echo "All arguments: ${args[@]}"
# Output: All arguments: hello world test

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
2
3
local long_opts="role:,args:,help"
local args=getopt -o ra --long $long_opts -n "$0" -- "$@"
eval set -- "${args}"

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
2
3
Example: ./myscript.sh --role=admin --args=val --help foo bar
Output of getopt:
--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
2
3
4
5
6
7
8
9
while true; do
case "$1" in
--role) ROLE=$2; shift 2 ;;
--args) ARGS=$2; shift 2 ;;
--help) show_help; exit 0 ;;
--) shift; break ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done

Summary

Line Purpose
long_opts=… Defines supported long options
getopt … Parses CLI arguments
eval set – … Makes parsed options available for normal while-case parsing