DRAFT Bash
A script file needs to be executable:
chmod u+x scriptnameexecutable for the userchmod a+x scriptnameexecutable for everyone- use
-xinstead of+xto remove the executable flag
In the first line it should contain a shebang (#!) indicating the script language:
#!/bin/bash
or
#!/usr/bin/env bash
Comments
Comments start with the pound symbol:
# This is a comment
echo "Hello" # and this is another one
Script arguments
- Script name:
$0 - Number of arguments:
$# - Arguments:
${1}…${n} - Arguments with default value (using
:-operator):${n:-value} - All arguments as a list:
$* - All arguments as a list:
$@
$? contains the exit status of the last invocation.
Fire And Forget
If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background, and these are referred to as asynchronous commands.
command &
Exit status
Every script and function returns the status code of the last executed instruction. That numeric value is:
0the code completed successfully1..255error code to signal a problem
It can also be returned explicitly with:
return codefinish the current function and continue the execution of the next instruction in the calling scriptexit codefinish the execution of the current shell
Variable
There cannot be a space before the equal = symbol, or it will be interpreted as a function call!
In general, when using variables favor both, quotes and curly brackets:
echo variable_nameecho $variable_name # nicerecho "${variable_name}other text here" # the nicest
DECLARE ??? (SET, UNSET, ENV)
CONSTANTS
INTEGERS
ARRAYS (and ASSOCIATIVE ARRAYS)
FUNCTIONS
EXPORT
Function local
f() {
local name=value
}
Local
name=value
Just for the call
name=value command arg1 arg2
Session
export name=value
Prompt user
Prompt the user with echo:
read -p "Enter a value:" variable_name
Prompt the user without echo (silent):
read -sp "Enter a value:" variable_name
Value checks
- Numbers:
-eq,-lt,-gt - Strings:
==(just=can also be used),!=,=~(regex check)
When writing direct checks (no if, while, etc) we need [] or [[]], except for regex checks (=~ and == with path style) that can only be used with [[]].
These brackets need to be surrounded by spaces!
TEST !!! (test or [[ or [ or (( ))
Existence tests:
-ffile-ddirectory-Lsymbolic link-eany of the above
Permission tests:
-rread-wwrite-xexecute-suser + s(pecial) (SUID bit)-ggroup + s(pecial) (SGID bit)-kother + t(sticky) (sticky bit)
Quick logic
&&will execute the second argument if the first one succeeds (AND)||will execute the second argument if the first one fails (OR)
IF
if [ check ] ; then
...
fi
if [ check ] ; then
...
else
...
fi
if [ check ] ; then
...
elif [ check ] ; then
...
else
...
fi
For oneliners an extra ; is needed after every command.
if [ check ] ; then ... ; fi
AND / OR
if [ check ] && [ check ] ; then
...
fi
if [ check ] || [ check ] ; then
...
fi
Modern arithmetic checks
The $ sign in variable names can be dropped
if (( numeric_check )); then
fi
FOR
for loop_variable in value_list ; do
...
done
C-style
for (( init ; check ; inc )) ; do
...
done
Array iteration idiom
for (( i=0 ; i<${#array[*]} ; i++ )) ; do
...
done
WHILE/UNTIL
while [ check ] ; do
...
done
until [ check ] ; do
...
done
BREAK/CONTINUE
CASE
Matches using Shell pattern matching rules.
case argument in
pattern )
...
;;
pattern )
...
;;
* )
...
;;
esac
case argument in
pattern | pattern )
...
;;
pattern | pattern | pattern )
...
;;
* )
...
;;
esac
EXEC ???
Functions
Function definition (should be ahead use):
function_name() {
...
}
alternative version
function function_name() {
...
}
Function arguments follow the same pattern as the script arguments.
Management
List of existing functions names: declare -F
View function definition: type function_name