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
HELLO

[root@vps ~]# echo WORLD
WORLD

[root@vps ~]# echo hello;echo world; date
hello
world
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.

AmpersandExample:

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

Example:

[root@vps ~]# pwd
/home/EcLinux

[root@vps ~]# cd /etc/

[root@vps ~]# echo $?
0

[root@vps ~]# pwd
/etc

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

[root@vps ~]# echo $?
1

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.

Example:

[root@vps ~]# echo HELLO && echo WORLD
HELLO
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.

Example:

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

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

6. Combining && and ||

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

Example:

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

Example:

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

Example:

[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!! 😛

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

2 thoughts on “Use of Control Operators in Unix/Linux command line

Leave a Reply

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