The symbol < is used for input redirection. Files, for example, can be used as input.
cat < file.txt
In this case, the file.txt is taken as the input, and the cat command then cats it out.
This symbol, known as the file redirection operator, is typically used to redirect the contents of a command/file to another by overwriting it.
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.
mcat file.txt 2> file2.txt
Here, the 2> means that the error will be dumped into file2.txt.
The symbol >> is used to append and not to replace! The file redirection operator replaces or overwrites everything while the >> is used to append.
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:
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”
The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol [email protected] 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.
The latter should chuck out a value of 2 because there are 3 elements (hello, world, and again).
This symbol redirects both the standard output and the standard error.
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.
if [ a \< b ]
echo “a is shorter than b”
echo “a is longer than b”
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
Symbol: [email protected] or $*
The symbol [email protected] is equivalent to $* which is equivalent to $1 $2 $3 $4…
echo $1 $2 $3 $4 $5
# the latter is equivalent to echo [email protected]
In this example, the $1, $2, $3, $4, and $5 are inputs from the command line. Alternatively, we could have written the following:
This particular symbol – $? – is used to get the exit status of the command previously passed.
echo “hello world” > file.txt
An exit status of 0 indicates that the process was completed successfully.
The symbol $$ stores the PID of the current shell.
In my case, it printed out the value 2443. This is the PID of the shell.
The symbol 2>&1 redirects both the standard output and the standard error to the standard output.
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!