title | description | created | updated |
---|---|---|---|
Bash shell |
Bourne-Again SHell, an `sh`-compatible command-line interpreter. |
2021-12-19 |
2021-12-19 |
#!/usr/bin/env bash
echo 'Hello from Bash!'
Declare the comment:
# my comment
Print the string with a trailing \n
:
echo 'Hello from Bash!'
# or
printf '%s\n' 'Hello from Bash!'
Read the string to a variable:
read my_variable
Declare the global/local variable:
declare my_variable='Hello from Bash!'
local my_variable='Hello from Bash!'
Remove the variable:
unset my_variable
Define Variables using export
export my_variable='Hello from Bash!'
Remove variable defined by export
export -n my_variable
Slice the variable:
echo ${my_variable:1:10}
echo ${my_variable:2}
echo ${my_variable:0:-2}
Increment/decrement the variable:
((my_variable++))
((my_variable--))
Calculate the number sum:
echo $((1 + 2))
Operator | Performs |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
** |
Exponentiation |
Calculate the number sum:
awk 'BEGIN { print 1.0 + 2.0 }'
Operator | Performs |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
** |
Exponentiation |
Match the string against a regular expresion:
[[ 'Hello from Bash!' =~ 'Bash' ]]
Extended regular expressions described.
Pattern | Matches |
---|---|
x? |
Zero or one x chars |
x* |
Any count x chars |
x+ |
One or more x chars |
x{n} |
n times x chars |
x{n,m} |
n to m times x chars |
x{n,} |
n or more times x chars |
[xy] |
x or y char |
[^xy] |
not x or y char |
Remove the shortest/longest matching pattern from beginning:
echo ${my_variable#*=}
echo ${my_variable##*=}
Remove the shortest/longest matching pattern from ending:
echo ${my_variable%*=}
echo ${my_variable%%*=}
Replace the first/all matching pattern:
echo ${my_variable/Bash/bash}
echo ${my_variable//Bash/bash}
Compare two variables:
if [[ $my_variable -lt $another_variable ]]; then
···
elif [[ $my_variable -eq $another_variable ]]; then
···
else
···
fi
Integer operator | Meaning |
---|---|
-lt |
[L]ess [t]han |
-eq |
[Eq]ual |
-gt |
[G]reater [t]han |
-le |
[L]ess than or [e]qual to |
-ge |
[G]reater than or [e]qual to |
-ne |
[N]ot [E]qual |
String operator | Meaning |
---|---|
== |
[Eq]ual |
!= |
[N]ot [E]qual |
File operator | Meaning |
---|---|
-f |
[F]ile exists |
-d |
[D]irectory exists |
-r |
File or directory exists and [r]eadable |
-w |
File or directory exists and [w]ritable |
-x |
File or directory exists and e[x]ecutable |
Iterate over the number range:
for i in {1,10}; do
...
done
Write the string with a trailing \n
to a file:
echo 'Hello from Bash!' > my_file
Write (append) the string with a trailing \n
to a file:
echo 'Hello from Bash!' >> my_file
Pass the first command stdout output as an input to a second command:
my_command | another_command
Replace the command invocation with its stdout output:
echo $(expr $my_variable + 1)
Replace the command invocation with a temporary file name with a command stdout output:
echo <(expr $my_variable + 1)
Declare the function:
my_function() {
···
}
Remove the function:
unset my_function
Defining arrays:
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
Iteration:
for i in "${arrayName[@]}"; do
echo $i
done