# Bash Cheat Sheet #### BASH stands for Bourne-Again SHell each BASH script should start with the shebang : \#!/bin/bash #### Comparison operators
**Operator** **Description** **Operator****Description**
-eqIs equal to -nIs not null
-neIs not equal to -zIs null
-gtIs greater than -rfile exists and is readable by the user
-geIs greater than or equal to -efile exists
-ltIs less then -ffile is a regular file (not directory or other special type)
-leIs less than or equal to -s file size is greater then 0
==is equal to -xfile is executable by the user
!=is not equal to -wfile is writable by the user
<Is less than !negates the operator
<=Is less than or equal to
>Is greater then
>=Is greater than or equal to
#### Variables for script arguments
**Variable** **Description** **Variable****Description**
$1 ... $nArgument number $#Number of arguments
$0Name of the file $?Return value (0 = ok, > 0 = error)
$\*List of arguments
#### Built in variables
**Variable** **Description** **Variable****Description**
$BASHpath to the bash binaries $BASH\_VERSIONversion of bash
$BASH\_ENVbash environment (used within the scripts) $BASH\_VERSINFOmore version details
$$process id of the current script $PATHpath variable
$EDITORdefault editor $UIDusers id
$EUIDeffective user id $FUNCNAMEname of the current function (executed from within the function)
$GROUPSgroups to which the current user belongs $HOMEhome directory of the current user
$HOSTTYPEsystem type $MACHTYPEmachine type
$LINENOline number $PWDprint working directory
$OLDPWDold print working directory $OSTYPEoperating system type
$REPLYdefault answer for a "read" command without explicit variable declaration $SECONDSthe time the script has run until this point
$RANDOMGenerate a random number between 0 and 32767
##### Declare and a few howtos **Declare a variable as read only** ``` declare -r my_read_only_variable=5 ``` **Declare a variable as an integer** ``` declare -i my_integer_variable=5 ``` **Declare a variable as an array** ``` declare -a myvar=(1 2 3 4 5) ``` **Declare a function and then run it** ``` declare -f myFunction myFunction(){ echo "my function runs" } myFunction ``` **Declare a variable and export it** ``` declare -x myvar=3 ``` #### Other commands
**Variable** **Description** **Variable****Description**