Converter Web ToolsConverter WebTools

Bash Scripting Cheat Sheet

This Bash scripting cheat sheet covers shell script syntax — variables, conditionals, loops, functions and special variables.

Variables & conditionals

CodeWhat it does
name="Tom"Set a variable
echo "$name"Use a variable
if [ "$x" -gt 5 ]; thenNumeric comparison
[ -f file ]True if file exists
[ -z "$s" ]True if string is empty
$( command )Command substitution

Loops, functions & specials

CodeWhat it does
for i in 1 2 3; doFor loop
while [ $x -lt 5 ]; doWhile loop
greet() { echo hi; }Define a function
$?Exit status of last command
$#Number of arguments
$@All arguments
$0Script name

Related tools & charts