Bash Scripting Essentials Guide
Bash Scripting Essentials Guide
standard output
make file output to file and to screen
file output a file
file output at the end of the file
/dev/null # descarta output
error
2>&1 # error a output
file file error
file error at the end of the file
2> /dev/null discard error
output and error
2>&1 | tee file both to file and to screen
>& file both to file
&>> file both at the end of the file
VARIABLES
environment variables
$PWD current working directory
$OLDPWD previous working directory
$PPID parent process identifier
$HOSTNAME computer name
$USER username
$HOME user directory
$PATH # command search routes
$LANG language for messages
$FUNCNAME function name in execution
$LINENO current line number (of the script)
$RANDOM random number
special variables
$0 script name
${N} parameter N
$$ current process identifier
$! identifier of the last process
$@ all the received parameters
$# number of parameters received
$? # (0=normal, >0=error) return code of the last command
shift # $1=$2, $2=$3, ... ${N-1}=${N}
ARRAYS
declare -a ARRAY array declaration
ARRAY=(value1 ... valueN) composite assignment
ARRAY[N]=valueN simple assignment
ARRAY=([N]=valorN valorM[P]=valorP) # assign cells N, M and P
${ARRAY[N]} cell value N
${ARRAY[*]} all the values
++ increase
-- decrease
numerical comparison operators
number1–eq number2 number1 equal to number2
number1–number2 number1 different from number2
number1 - lt number2 number1 less than number2
number1 - the number2 number1 less than or equal to number2
number1 -gt number2 number1 greater than number2
number1–the number2 number1 greater than or equal to number2
logical operators
! NOT
&& , -a AND
|| , -o OR
file operators
-e file exists
-s file is not empty
-f file normal
-d file directory
-h file symbolic link
-r file reading permission
-w file write permission
-x file execution permit
The file owner
-G file belongs to the group
f1-ef f2 f1 and f2 links same file
f1-nt f2 f1 newer than f2
f1-ot f2 f1 older than f2
string operators
-n string not empty
-z chain empty
string1 = string2 string1 equals string2
string1 == string2 string1 equals string2
string1 != string2 string1 different from string2