If you are looking for cheat sheets for bash scripting, then this section is for you.
The escape or special characters are used in the format string. Here is a list of the most common escape or special characters.
1
2
3
4
5
6
7
# \\ Displays a backslash character.
# \b Displays a backspace character.
# \n Displays a new line.
# \r Displays a carriage return.
# \t Displays a horizontal tab.
# \v Displays a vertical tab.
echo -e 'Hello\nWorld'
echo is a simple command but with limited capabilities. To have more control over the formatting of the output, use the following command instead.
1
2
3
4
5
# %s ==>> string.
# %d ==>> digits.
# %.3f ==>> floting value, 3 value will be accepted after the dot.
# %03d ==>> add 3 zeros as prefix of any number.
printf 'name: %s, age: %d, score: %.3f, role number: %03d\n' Yafiz 99 8.341123 847
We do have many variables in Linux, but I am gonna show you the most common variables only.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
echo $HOME # or ~ both will return the user's home directory path.
echo $PWD # return current working directory path.
echo $OLDPWD # return old working directory path.
echo $RANDOM # return random numbers every time you run.
echo $SHELL # return current or default interpreter.
echo $OSTYPE # return current operating system name.
# set condition with ostype variable
# if linux then install with apt package manager.
# if macos then install with brew package manager.
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "apt install -y nginx"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "brew install nginx"
fi
# if your program has a dictionary, brace expansion and etc,
# then use this condition to tell user
# this script requires Bash v4.0 or higher.
# and you have a smaller version.
if ((BASH_VERSINFO < 4)); then
printf '%s\n' "Error: This script requires Bash v4.0 or higher. You have version ${BASH_VERSION/(?)-release/}." 1>&2
exit
fi
# export allows you to use variables anywhere in the bash scripting,
# either in a file or directly on a shell.
# to set a global variable that can be used anywhere.
export my_fake_ip='192.168.1.10'
# remove or delete the variable if you don't need it anymore.
unset my_fake_ip
# tr command in linux
# complete article can be found here.
# https://deletify.app/tech-world/tr-command-in-linux/
echo "UPPERCASE" | tr A-Z a-z # convert lowercase.
echo "lowercase" | tr a-z A-Z # convert uppercase.
echo "Hello" | rev # reverse your string.
# calculate the parent directory of a given file or directory path.
dirname "/path/to/filename.txt" # ==>> will return /path/to/.
# return only the filename from a given path.
basename "/path/to/filename.txt"
# cut command in linux
# complete article can be found here.
# https://deletify.app/tech-world/cut-command-in-linux/
basename /path/to/filename.txt | cut -d. -f1 # ==>> filename
basename /path/to/filename.txt | cut -d. -f2 # ==>> txt
# Shortcut commands to check the history
!$ # return last argument from your last command.
!^ # return first argument from your last command.
!* # return all arguments from your last command.
!! # return the last most command back including arguments whatever were passed last time.
!n # return x number of commands back from history.