Quick reference

Shell Scripting Cheat Sheet

A practical guide to Shell scripting, covering core knowledge such as variable handling, flow control, and function writing to help operations engineers master Linux automation script development skills.

· Published · Updated

Scope. Bash scripting. Run with bash script.sh when using Bash-specific features. Quote expansions such as "$value", use $(command) for command substitution, and use "$@" to preserve separate script arguments.

Variables

Command Description
VAR=value Define a variable
$VAR Access a variable
`command` Command substitution

Flow Control

Command Description
if Conditional check
then Execute if condition is true
else Execute if condition is false
elif Alternative condition
fi End conditional check
for Loop
while Loop while condition is true
do Loop body
done End loop

Functions

Command Description
function_name() {} Define a function
return Return from a function
$? Return value of the last command

Script Control

Command Description
#!/bin/bash Specify the interpreter used by the script
chmod +x script.sh Grant execute permission to the script
./script.sh Execute the script

Official reference

Related topics