Bash Scripting Flashcards
(Single Parentheses)
Runs the command(s) in a subshell and returns a single exit code. Any variables declared only exist in this subshell.
((Double Parentheses))
– Used for integer arithmetic. Any variables modified inside the brackets stick.
– It doesn’t return a value, but if the result is non- zero, it returns a 0, i.e., a “successful” exit code
– If the result inside is zero, it returns an exit code of 1, i.e., a “not successful” exit code
$( Dollar Single Parentheses )
This is used for interpolation of the subshell command output into a string (instead of just an exit code if without the dollar symbol)
$(( Dollar Double Parentheses ))
This performs arithmetic interpolation of the command in the brackets, e.g., the output of the command is put into a string as opposed to just the exit code. Strictly for integer arithmetic.
[ Single Square Brackets ]
– This is a shorthand version of the built-in test command. The commands inside are executed and checked if true or false.
– Strings of zero length are false and strings of length 1 or more are true
[[ Double Square Brackets ]]
– Also used for true/false testing
– Supports regular expression matching
– Preferable to use over single brackets in the vast majority of use cases
{ Single Curly Braces }
– Used for expansion
– For example, echo {1..10} makes a range
– echo “This is “{great,useful}”.” returns This is great. This is useful.
${dollar braces} (note no spaces)
This is for variable interpolation when normal string interpolation can’t work correctly
Positional Arguments
– Arguments passed to a script are processed in the same order in which they’re sent.
– The indexing of the arguments starts at one, and the first argument can be accessed inside the script using $1. Similarly, the second argument can be accessed using $2, and so on.
– The positional parameter refers to this representation of the arguments using their position.
Flags
– Using flags is a common way of passing input to a script.
– When passing input to the script, there’s a flag (usually a single letter) starting with a hyphen (-) before each argument.\– For example, ls -l lists all the files in a directory – the -l is a flag to the ls command
– The bash shell provides a built-in getopts command to help with handling flag options
Integer comparison operators
-eq is equal to
-ne is not equal to
-gt is greater than
-ge is great than or equal to
-lt is less than
-le is less than or equal to
- Usage example: if [ “$a” -gt “$b” ] then
- Note <, <=, >, >= can be used within double parentheses, e.g., ((“$a” > “$b”))
String Comparison Operators
= or == is equal to
!= is not equal to
< is less than, in ASCII alphabetical order
> is greater than, in ASCII alphabetical order
-z string is null
-n string is not null
File Test Operators
-e file exists
-f file is a regular file (not a dir/device)
-d file is a directory
-h or -L file is a symbolic link
-r file has read permission (for current user)
-w file has write permission (for current user)
-x file has execute permission (for current user)
- Example usage: if [ -d “$path” ] then
Flow of Controls: If Statements
if CONDITION then
COMMANDS
else
OTHER_COMMANDS
fi
Flow of Controls: While Loops
while [ CONDITION ]
do
COMMANDS
done