Show Menu
Cheatography

Bash Scripting Cheat Sheet (DRAFT) by

Bash Scripting Cheat Sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Variables

$0
Name of the bash script
$1 - $9
The first 9 arguments to the bash script
$#
Amount of arugments passed to the bash script
$@
All the arguments supplied to the bash script
$?
The exit status of the most recently run process
$$
The process ID of the current script
$USER
The username of the user running the script
$HOSTNAME
The hostname of the machine the script is running on
$SECONDS
The number of seconds since the script was started
$RANDOM
Returns a different random number each time is it referred to
$LINENO
Returns the current line number in the Bash script
variab­le=­value
Set value to variable
echo $variable
Returns value of the variable
sample­dir­=/etc
Set path as value to variable
ls $sampledir
Returns content of /etc
echo
Return empty line
echo "­$va­ria­ble­"
Returns value of variable
echo '$vari­able'
Returns '$vari­able'
variab­le=$( ls /etc | wc -l )
Saves output of command to variable
export variable
Make variable available to child processes

Input

 
 

Arithmetic

 

If Statements

 

Loops

 

Functions

 

User Interface