How Can We Use Operators In Linux and Unix CLI?

317332_1bc6_4

A token that performs a control function. It is a newline or one of the following:

Ampersand ( & ) 

When a line ends with ampersand &, the shell will not wait for the command to finish. You will get your shell prompt back, and the command is executed in background.

for example :

Ampersand

Semicolon (;)

You can put two or more commands on the same line separated by a Semicolon ; . Both series will be executed sequentially with the shell waiting for each command to finish before starting the next.

For example :

semi

Double Ampersand (&&)

When using && the second command is executed only if the first one succeeds.

For example:

double_ampersand

Double vertical bar (||)

The || represents a logical OR. The second command is executed only when the first command is fail.

For example :

double_vertical

Combine && with ||

You can use this logical AND and logical OR to write an if-then-else structure on the command line.

For example:

combine

Pound sign (#)

Everything written after a pound sign (#) is ignored by the shell. This is useful to write a shell comment, but has no influence on the command execution or shell expansion.

For example:

pound

Escaping special characters

The backslash character enables the use of control characters, but without the shell interpreting it, this is called escaping characters.

For example:

backslash

End of line backslash

Lines ending in a backslash are continued on the next line. The shell does not interpret the newline character and will wait on shell expansion and execution of the command line until a newline without backslash is encountered.

For example :

echo

Dollar Question mark ($?)

The exit code of the previous command is stored in the shell variable $?. Actually $? is a shell parameter and not a variable. This parameter is used to check the last executed command status. If the value returned by $? is 0, that was a success one otherwise that was a failed one.

dollar

0 – Represents a successful command.
1 or any-other non zero – Represents a failed output.

You can use the above operators in your server administration’s works or script that running on client 🙂

make me happy with your comments if you have any questions 🙂