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:

[[email protected] ~]# echo HELLO

[[email protected] ~]# echo WORLD

[[email protected] ~]# 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.


[[email protected] ~]# sleep 40 &
[1] 4845

[[email protected] ~]# ^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.


[[email protected] ~]# pwd

[[email protected] ~]# cd /etc/

[[email protected] ~]# echo $?

[[email protected] ~]# pwd

[[email protected] ~]# cd /root/arun
bash: cd: /root/arun: Permission denied

[[email protected] ~]# 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.


[[email protected] ~]# echo HELLO && echo WORLD

[[email protected] ~]# 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.


[[email protected] ~]# echo HELLO || echo WORLD

[[email protected] ~]# 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.


[[email protected] ~]# touch linux.txt

[[email protected] ~]# rm linux.txt && echo It worked || echo It failed
It worked

[[email protected] ~]# 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.


[[email protected] ~]# mkdir test # we create a directory

[[email protected] ~]# cd test #### we enter the directory

[[email protected] ~]# 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.


[[email protected] ~]# echo hello ; world
hello ; world

[[email protected] ~]# echo hello world
hello world

[[email protected] ~]# echo escaping \ # & " '
escaping # & " '

[[email protected] ~]# 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.

[[email protected] ~]# 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

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

2 Responses

  1. rakesh kumar says:


Leave a Reply

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