Use of Control Operators in Unix/Linux command line

A token that performs a control function. It is a newline or one of the following: ‘||’, ‘&&’, ‘&’, ‘;’, ‘;;’, ‘|’, ‘|&’, ‘(’, or ‘)’. We can put more than one commands on the command line using Control Operators. In this topic I’m explaining the usage of control operators in Linux CLI.

1. 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.

Please see the examples:

[root@vps ~]# echo HELLO

[root@vps ~]# echo WORLD

[root@vps ~]# echo hello;echo world; date
Thu Dec 10 01:25:01 EST 2015

2. 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.


[root@vps ~]# sleep 40 &
[1] 4845

[root@vps ~]# ^C
[1]-  Done                    sleep 40

3. 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.


[root@vps ~]# pwd

[root@vps ~]# cd /etc/

[root@vps ~]# echo $?

[root@vps ~]# pwd

[root@vps ~]# cd /root/arun
bash: cd: /root/arun: Permission denied

[root@vps ~]# echo $?

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

4. Double Ampersand (&&)

The shell will interpret && as a loical AND. When using && the second command is executed only if the first one succeeds.


[root@vps ~]# echo HELLO && echo WORLD

[root@vps ~]# wecho HELLO && echo WORLD
bash: wecho: command not found

5. Double vertical bar (||)

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


[root@vps ~]# echo HELLO || echo WORLD

[root@vps ~]# zecho HELLO || echo WORLD
bash: zecho: command not found

6. Combining && and ||

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


[root@vps ~]# touch linux.txt

[root@vps ~]# rm linux.txt && echo It worked || echo It failed
It worked

[root@vps ~]# rrrrm linux.txt && echo It worked || echo It failed
bash: rrrrm: command not found
It failed

7. 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.


[root@vps ~]# mkdir test # we create a directory

[root@vps ~]# cd test #### we enter the directory

[root@vps ~]# ls # is it empty ?

8. Escaping special characters

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


[root@vps ~]# echo hello ; world
hello ; world

[root@vps ~]# echo hello world
hello world

[root@vps ~]# echo escaping \ # & " '
escaping # & " '

[root@vps ~]# echo escaping \?*"'
escaping ?*"'

9. 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.

[root@vps ~]# echo This command line \
> is split in three \
> parts
This command line is split in three parts

That’s it. Use the above operators in your Shell scripts… Njoy!! 😛

Arunlal Ashok

DevOps (Server & Cloud infrastructure) Engineer. I'm dealing Linux servers since 2012. I started this blog to share and discuss my ideas. Always happy for an open discussion! Write to arun (@) crybit (dot) com

You may also like...

2 Responses

  1. rakesh kumar says:


Leave a Reply

Your email address will not be published. Required fields are marked *