This is a small bash handbook with examples. I made it just for fun, practice and not forget the bash basics..
- Stream
- Redirections
- Lists of Commands
- Pipe
- Expansion
- Variables
- Arrays
- Arguments
- Arithmetic
- Conditional
- Loops
- Functions
- Exit
- Builtins
- Commands
These are three standard streams that are established when a Linux command is executed.
Code | Descriptor | Description |
---|---|---|
0 |
stdin |
The standard input. |
1 |
stdout |
The standard output. |
2 |
stderr |
The errors output. |
Operator | Description |
---|---|
< |
Redirecting Input |
> |
Redirecting Output |
[&>][>&][>word 2>&1] |
Redirecting Standard Output and Standard Error |
>> |
Appending Redirected Output |
[&>>][>>word 2>&1] |
Appending Standard Output and Standard Error |
<< |
Here Documents |
<<< |
Here Strings |
# Redirecting Output
$ ls > /dev/stdout
# Redirecting Standard Output and Standard Error
$ ls &> /dev/stdout
$ ls > /dev/stdout 2>&1
$ ls 2>&1 > /dev/stdout
# Here Documents
$ cat >&2 <<END_USAGE
main: Cheat Sheet Bash Script
USAGE:
main.sh [FLAGS]
FLAGS:
-h, --help Prints help information
END_USAGE
# Here Strings
string="This is a string of words."
read -r -a Words <<< "$string"
echo "First word in String is: ${Words[0]}" # This
echo "Second word in String is: ${Words[1]}" # is
echo "Third word in String is: ${Words[2]}" # a
echo "Fourth word in String is: ${Words[3]}" # string
echo "Fifth word in String is: ${Words[4]}" # of
echo "Sixth word in String is: ${Words[5]}" # words.
echo "Seventh word in String is: ${Words[6]}" # (null)
# command2 is executed if, and only if, command1 returns an exit status of zero (success).
command1 && command2
# command2 is executed if, and only if, command1 returns a non-zero exit status.
command1 || command2
The Pipe is a command in Linux that lets you use two or more commands such that output of one command serves as input to the next.
ls -al | grep ".sh"