w3resource

Linux - Control-operators

Introduction

In this session we have covered various control operators like semicolon (;), ampersand (&), dollar question mark ($?), double ampersand (&&), double vertical bar (||), combining (&&) and (||), pound sign (#), escaping special characters (\), end of line backslash etc. We also briefly discuss related parameters ($?) and similar special characters(&).

semicolon(;)

You can put two or more commands on the same line separated by a semicolon(;) . The shell will scan the line until it reaches the semicolon. All the arguments before this semicolon will be considered a separate command from all the arguments after the semicolon. Both series will be executed sequentially with the shell waiting for each command to finish before starting the next one.

 datasoft@datasoft-linux:~$ echo Ram
Ram
datasoft@datasoft-linux:~$ echo Shyam
Shyam
datasoft@datasoft-linux:~$ echo Ram ; echo Shyam
Ram
Shyam
datasoft@datasoft-linux:~$ 

ampersand (&)

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 the background. You will get a message when this command has finished executing in the background.

datasoft@datasoft-linux:~$ sleep 20 &
[1] 3124
datasoft@datasoft-linux:~$ 
[1]+  Done        sleep 20

The technical explanation of what happens, in this case, is explained in the chapter about processes.

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

datasoft@datasoft-linux:~$ touch file1
datasoft@datasoft-linux:~$ echo $?
0
datasoft@datasoft-linux:~$ rm file1
datasoft@datasoft-linux:~$ echo $?
0
datasoft@datasoft-linux:~$ rm file1
rm: cannot remove ?file1?: No such file or directory
datasoft@datasoft-linux:~$ echo $?
1
datasoft@datasoft-linux:~$

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

 datasoft@datasoft-linux:~$ echo Ram  && echo Shyam
Ram
Shyam
datasoft@datasoft-linux:~$ zecho Ram && echo Shyam
No command 'zecho' found, did you mean:
 Command 'aecho' from package 'netatalk' (universe)
 Command 'echo' from package 'coreutils' (main)
zecho: command not found

The following example (logical AND principle) starts with a working cd followed by ls, then a non-working cd which is not followed by ls.

datasoft@datasoft-linux:~$ cd gen && ls
bash: cd: gen: No such file or directory
datasoft@datasoft-linux:~$ cd gen && ls
bash: cd: gen: No such file or directory

double vertical bar (||)

The || represents a logical OR. The second command is executed only when the first command fails (returns a non-zero exit status).

datasoft@datasoft-linux:~$ echo Ram || echo Shyam ; echo Madhu
Ram
Madhu
datasoft@datasoft-linux:~$ zecho Ram || echo Shyam; echo Madhu
No command 'zecho' found, did you mean:
 Command 'echo' from package 'coreutils' (main)
 Command 'aecho' from package 'netatalk' (universe)
zecho: command not found
Shyam
Madhu
datasoft@datasoft-linux:~$ 

Here is another example of the same logical OR principle.

datasoft@datasoft-linux:~$ cd gen || ls
bash: cd: gen: No such file or directory
ABC.png                             MyTest
ajax-php-mysql-user-interface.html  part1
count                               part2
Desktop                             part3
Documents                           Pictures
Downloads                           png
examples.desktop                    Public
file2                               sqlite3
FileA                               sqlite-amalgamation-3080500 (2)
FileB                               sqlite-amalgamation-3080500.zip
linux-command-past-date.png         sqlite-shell-linux-x86-3080500.zip
mno.txt                             summer.png
Music                               Summer.png
MyDir                               Templates
MyDir1                              test1
MyDirA                              text2
Myfile1.doc                         typescript
MYFILE1.doc                         Videos
MYFILE2.doc                         xyz.txt
datasoft@datasoft-linux:~$

combining (&&) and (||)

You can use this logical AND and logical OR to write an if-then-else structure on the command line. This example uses echo to display whether the rm command was successful.

datasoft@datasoft-linux:~$ rm file1 && echo It worked! || echo It failed!
rm: cannot remove ?file1?: No such file or directory
It failed!
datasoft@datasoft-linux:~$ rm file1 && echo It worked! || echo It failed!
rm: cannot remove ?file1?: No such file or directory
It failed!
datasoft@datasoft-linux:~$

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.

datasoft@datasoft-linux:~$ mkdir test2    # we create a directory
datasoft@datasoft-linux:~$ cd test2      #### we enter the directory 
datasoft@datasoft-linux:~/test2$ ls      # is it empty?
datasoft@datasoft-linux:~/test2$

escaping special characters (\)

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

datasoft@datasoft-linux:~/test2$ echo Ram \; Shyam
Ram ; Shyam
datasoft@datasoft-linux:~/test2$ echo Ram\ \ \ Shyam
Ram   Shyam
datasoft@datasoft-linux:~/test2$ echo escaping \\\ \#\ \"\ \'
escaping \ # " '
datasoft@datasoft-linux:~/test2$ echo escaping \\\?\*\"\'
escaping \?*"'
datasoft@datasoft-linux:~/test2$

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.

datasoft@datasoft-linux:~/test2$ echo Ram is \
> good \
> boy
Ram is good boy
datasoft@datasoft-linux:~/test2$

another example of the end of line backslash

datasoft@datasoft-linux:~/test2$ echo End of\
> line \
> backslash
End ofline backslash
datasoft@datasoft-linux:~/test2$

Exercise and Solution:

1. When you type passwd, which file is executed?

which passwd

2. What kind of file is that?

Code:

file /usr/bin/passwd

3. Execute the pwd command twice. (remember 0.)

Code:

pwd ; pwd

4. Execute ls after cd /etc, but only if cd /etc did not error.

Code:

cd /etc && ls

5. Execute cd /etc after cd etc, but only if cd etc fails.

Code:

cd etc || cd /etc

6. Execute sleep 7, what is this command doing?

pausing for seven seconds

7. Execute sleep 500 in the background.

sleep 500 &

8. Write a command line that executes rm abcd. Your command line should print 'success' if abcd is removed, and print 'failed' if there was a problem.

Code:

rm abcd && echo success || echo failed

Previous: Linux Commands
Next: Linux - Working with directories



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/linux-system-administration/control-operators.php