Symbol: <
The symbol < is used for input redirection. Files, for example, can be used as input.
For example:
cat < file.txt
In this case, the file.txt is taken as the input, and the cat command then cats it out.
Symbol: >
This symbol, known as the file redirection operator, is typically used to redirect the contents of a command/file to another by overwriting it.
For example:
echo “hello world’ > file.txt
Here, the > symbol is similar to 1>. This is so because the 1 is a file descriptor for the standard output. Please note that the file descriptors are as follows:
0 — Standard input, stdin
1 — Standard output, stdout
2 — Standard error, stderr
In the previous scenario, the single forward arrow was equivalent to 1>. However, we can also write 2> to forward it to the standard error.
For example:
mcat file.txt 2> file2.txt
Here, the 2> means that the error will be dumped into file2.txt.
Symbol: >>
The symbol >> is used to append and not to replace! The file redirection operator replaces or overwrites everything while the >> is used to append.
For example:
echo “this is the second line” >> file.txt
echo “this is the third line” >> file.txt
The latter will append the two lines to the file called file.txt. The result of file.txt will then be as follows:
Symbol: #
The hashtag is used to add one-line comments into scripts. These comments are not executed/run.
# this will dump the line into the file
echo “this is a file” > file.txt
Unlike the #, which is a one-liner, the multi-line comments look more like this;
: ‘
This is the comments section
This is the first line
This is the second line
‘
echo “hello world”
Symbol: $#
The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol $@ or simply $1, $2, etc., is used, we ask for command-line input and store their values in a variable. The symbol $# is used to retrieve the total number of arguments passed.
For example:
The latter should chuck out a value of 2 because there are 3 elements (hello, world, and again).
Symbol: &>
This symbol redirects both the standard output and the standard error.
For example;
In this case, the &> symbol redirects both the standard output and the standard error to the file called file.txt. Thus, both the output generated and the error generated is placed in the same file.
Symbol: \< and \>
You need to compare the string length or character lengths; this can be done via the symbols \< and \>. These two symbols are used to compare character lengths.
For example:
a=”cat”
b=”lynx”
if [ a \< b ]
then
echo “a is shorter than b”
else
echo “a is longer than b”
fi
In this case, the word stored in a – or cat – has a character length of 3, while the word stored in b – or lynx -has a character length of 4. Thus the answer should be that “a is shorter than b.”
Symbol: ^^, ^ and ,,
Some symbols function to change the case of the characters.
^^ — to turn all characters to uppercase
^ — to turn the first letter to uppercase
,, — to turn all characters to all lowercase
For example:
a=”cat”
b=”lynx”
c=”DRAGON”
echo ${a^^}
echo ${b^}
echo ${c,,}
Symbol: $@ or $*
The symbol $@ is equivalent to $* which is equivalent to $1 $2 $3 $4…
Ex:
echo $1 $2 $3 $4 $5
# the latter is equivalent to echo $@
In this example, the $1, $2, $3, $4, and $5 are inputs from the command line. Alternatively, we could have written the following:
echo $@
Or
echo $*
Symbol: $?
This particular symbol – $? – is used to get the exit status of the command previously passed.
Ex:
echo “hello world” > file.txt
echo $?
An exit status of 0 indicates that the process was completed successfully.
Symbol: $$
The symbol $$ stores the PID of the current shell.
For example:
echo $$
In my case, it printed out the value 2443. This is the PID of the shell.
Symbol: 2>&1
The symbol 2>&1 redirects both the standard output and the standard error to the standard output.
For example:
ls 2>&1 > file.txt
In this case, all the standard output and if any error is generated, the standard error is both directed into the file called file.txt.
Bash scripting is a key scripting language that can be used to automate tasks. During bash scripting, we encounter much code, but we also encounter special characters or symbols that are unique to bash. These symbols each have a particular role in bash scripting, and they aren’t always obvious. In this tutorial, we reviewed a few key symbols used while writing bash scripts. Obviously, there are many symbols out there; however, some are encountered so frequently that it might be necessary to know them while bash scripting. So go forth, fearless of the symbol from here onwards!
Happy Coding!