Skip to content

Shell Scripting Cheat Sheet

Shell Scripting Cheat Sheet

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