Bash Scripting: Symbols – Linux Hint

In Linux, the terminal is everything, it’s where we pass commands and it’s where we pass scripts. Therefore, one of the most important scripting languages is bash. Bash scripting is used to automate boring tasks on Linux. To automate tasks, commands are written within the script and given a .sh extension. As part of the commands, certain symbols are also used. These symbols are unique to bash, and each has its own specific meaning. In this tutorial, we will review the various symbols encountered during bash scripting and their specific meaning.

Symbol:

<

The < symbol is used for input redirection. Files, for example, can be used as input.

For example:

In this case, the file.txt is taken as input, and the cat command deletes it

.

<img

src=”https://linuxhint.com/wp-content/uploads/2022/02/untitled_14.png” alt=”Untitled_14″ /> Symbol: >

This symbol, known as the file redirection operator, is typically used to redirect the contents of one command/file to another by overwriting it.

For example

:

<img src="https://linuxhint.com/wp-content/uploads/2022/02/untitled.png" alt="Untitled" /

>

Here, the > symbol is similar to 1>. This is because 1 is a file descriptor for standard output. 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 forward arrow was equivalent to 1>. However, we can also type 2> to forward it to the standard error.

For example

:

Here, the 2> means that the error will be dumped in file2.txt.

<img src="https://linuxhint.com/wp-content/uploads/2022/02/untitled2.png" alt="Untitled2" />

Symbol:

>>

The >> symbol is used to add and not to replace! The file redirection operator replaces or overwrites everything while using the >> to append.

For example:

The latter will add the two lines to the file named file.txt. The output of the file.txt will be as follows

:

Untitled3

Symbol: #

The hashtag is used to add one-line comments in scripts. These comments are not executed/executed.

Untitled4 Unlike #,

which is a single line, multiline comments look more like this;

Untitled5

Symbol:

$# The $#

symbol is used to retrieve the length or number of arguments passed through the command line. When using the $@ symbol or just $1, $2, etc., we request command line input and store its values in a variable. The $# symbol is used to retrieve the total number of arguments passed.

For example:

The latter should remove a value of 2 because there are 3 elements (hello, world, and again).

<img src="https://linuxhint.com/wp-content/uploads/2022/02/untitled7.png" alt="Untitled7" /

>

Symbol: &>

This symbol redirects both standard output and standard error.

For example

;

In this case, the &> symbol redirects both the standard output and the standard error to the file named file.txt. Therefore, both the output generated and the error generated are placed in the same file.

<img src=”https://linuxhint.com/wp-content/uploads/2022/02/untitled6.png” alt=”Untitled6″ /

>

Symbol:

\< and \> You must compare the length of the

string or the length of the characters; this can be done through the symbols \< and \>. These two symbols are used to compare character lengths.

For example:

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. Therefore, the answer should be that “a is shorter than b”.

Untitled8 Symbol: ^^, ^ and ,,

Some symbols work to change

the case of characters. ^^ — convert all characters to uppercase

^ — convert the first letter

to uppercase

,, — convert all characters to lowercase

For example

:

Untitled9

Symbol:

$@ or $*

The symbol $@ is equivalent to $

*

which equals $1 $2 $3 $4…

Ex:

In this example, $1, $2, $3, $

4, and $5 are command-line entries. Alternatively, we could have written the following

:

O

Untitled_10

Symbol

: $?

This particular symbol – $? – is used to get the output state of the previously passed command

.

Ex

:

An output status of 0 indicates that the process completed successfully

.

Untitled_11

Symbol

: $$

The $$ symbol stores the PID of the current shell

.

For example:

In my case, it printed the value 2443. This is the PID of the shell.

<img src="https://linuxhint.com/wp-content/uploads/2022/02/untitled_12.png" alt="Untitled_12" /

> Symbol: 2>&1

The symbol 2>&1 redirects both

the standard output and the standard error to the standard output.

For example:

In this case, all standard output and if any error is generated, the standard error is directed to the file named file.txt

.

Untitled_13

Bash scripting is a key scripting language that can be used to automate tasks. During bash scripting, we find a lot of code, but we also find special characters or symbols that are unique to bash. Each of these symbols has a particular role in bash scripting, and they are not always obvious. In this tutorial, we review some key symbols used when 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 relying on scripting. So go ahead, without fear of the symbol from here on out!

Happy coding!