Hi and welcome to our blog. In this tutorial we will learn about Linux control operators. We will discuss special characters and their meanings. We will discuss about
semi colon (;), ampersand (&) , dollar sign ($), question mark (?) double ampersand (&&) logical And , logical OR (||) escaping special characters using \.
Let us begin with semicolon
semicolon ” ; “
If you want to write more than one commands on same line you can separate commands by using semicolon ; Linux shell will start scanning line until it reaches
semicolon. Every thing before this semicolon will be taken as separate command and will be executed.Then the next following command will be executed and so its
sequential process executing command one by one.
[root@shahid ~]# echo Shahid Shahid [root@shahid ~]# echo $HOSTNAME shahid.localdomain [root@shahid ~]# echo I Love; echo linux; echo this is name of this machine; echo $HOSTNAME I Love linux this is name of this machine shahid.localdomain [root@shahid ~]#
ampersand &
If you want to execute command in background and want the shell prompt back without waiting for that command to be processed then you can use character & (ampersand)
And you will be given message once that background command has finished processing
When a line ends with an 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. You will get a message when this command has finished executing in background.
[root@shahid ~]# sleep 5 & [1] 6314 [root@shahid ~]# echo this is sample output i am waiting for 5 seconds this is sample output i am waiting for 5 seconds [root@shahid ~]# [1]+ Done sleep 5 [root@shahid ~]#
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, since you cannot assign a value to $?.
[root@shahid ~]# touch shahid.txt [root@shahid ~]# echo $? 0 [root@shahid ~]# rm shahid.txt rm: remove regular empty file âshahid.txtâ? y [root@shahid ~]# echo $? 0 [root@shahid ~]# rm shahid.txt rm: cannot remove âshahid.txtâ: No such file or directory [root@shahid ~]# echo $? 1 [root@shahid ~]#
double ampersand &&
The shell will interpret && as a logical AND. When using && the second command is executed only if the first one succeeds (returns a zero exit status).
Double ampersand && is interpreted as logical AND . Which means that if first command returns 0(successful execution) second command will run
[root@shahid ~]# echo london && echo manchester london manchester [root@shahid ~]# city london && echo manchester bash: city: command not found... [root@shahid ~]#
Lets see another example. Suppose you want to list contents of directory and your current working directory is not that directory. you will need to cd and then list
[root@shahid ~]# cd box && ls -bash: cd: box: No such file or directory [root@shahid ~]#
double vertical bar ||
Logical OR is represented by || (double bar).
The || represents a logical OR. The second command will be executed even if first fails (returns non-zero)
[root@shahid ~]# echo shahid || echo Apple ; echo orange shahid orange [root@shahid ~]# eat banana || echo Apple ; echo orange bash: eat: command not found... Apple orange [root@shahid ~]#
Lets see example of logical or operator with changing into non working directory and listing contents.
[root@shahid ~]# ls alias.txt anaconda-ks.cfg epoch_converter.pl initial-setup-ks.cfg test [root@shahid ~]# cd linuxmachine || ls -bash: cd: linuxmachine: No such file or directory alias.txt anaconda-ks.cfg epoch_converter.pl initial-setup-ks.cfg test [root@shahid ~]#
In above example we did ls to see whats in current working directory. Then we tried to cd into linuxbox directory which was not there so first command failed and
second command after || listed contents of current working directory.
using $$ and ||
To use if-then-else structure on the command line you can use logical AND (&&) and logical OR (||). Below example will help to understand this concept
We will use rm command to remove file then it will display second command of logical and and if it fails it will display it failed (second part of OR command)
[root@shahid ~]# rm alias.txt && echo file removed successfully || echo file could not be removed as it does not exist rm: remove regular empty file âalias.txtâ? y file removed successfully [root@shahid ~]# rm alias.txt && echo file removed successfully || echo file could not be removed as it does not exist rm: cannot remove âalias.txtâ: No such file or directory file could not be removed as it does not exist [root@shahid ~]#
Comments in shell commands using pound sign (#)
you can use pound sign (#) to use comments. Any thing after # will be taken as comment and it will not be processed by shell
[root@shahid ~]# date #this command displays date Sun 13 Mar 11:33:44 GMT 2016 [root@shahid ~]#
Using \ to escape special character
If you want shell not to interpret special characters which shell use to execute you can use \ before that special character and shell will ignore processing it
[root@shahid ~]# echo linux \; is amazing linux ; is amazing [root@shahid ~]#
using \ at end of line
If you want to split your linux commands into multiple lines then you can use \ at end of line. Shell will not execute line ending with \ it will wait for shell
expansion until it finds line with no \ . then it will execute the whole command
[root@shahid ~]# echo this command \ > is split \ > into multiple lines this command is split into multiple lines [root@shahid ~]#
Thanks for visiting our blog. In this tutorial we have learnt special characters and their meanings. We discussed with examples
semi colon (;), ampersand (&) , dollar sign ($), question mark (?) double ampersand (&&) logical And , logical OR (||) escaping special characters using \.
feel free to subscribe to our free newsletter and feel free to post suggestion or mention any mistakes in this tutorial.