Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.

 [email protected]:~$ echo Ram
Ram
[email protected]:~$ echo Shyam
Shyam
[email protected]:~$ echo Ram ; echo Shyam
Ram
Shyam
[email protected]:~$ 

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.

[email protected]:~$ sleep 20 &
[1] 3124
[email protected]:~$ 
[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 $?.

[email protected]:~$ touch file1
[email protected]:~$ echo $?
0
[email protected]:~$ rm file1
[email protected]:~$ echo $?
0
[email protected]:~$ rm file1
rm: cannot remove ?file1?: No such file or directory
[email protected]:~$ echo $?
1
[email protected]:~$

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

 [email protected]:~$ echo Ram  && echo Shyam
Ram
Shyam
[email protected]:~$ 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.

[email protected]:~$ cd gen && ls
bash: cd: gen: No such file or directory
[email protected]:~$ 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).

[email protected]:~$ echo Ram || echo Shyam ; echo Madhu
Ram
Madhu
[email protected]:~$ 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
[email protected]:~$ 

Here is another example of the same logical OR principle.

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

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.

[email protected]:~$ rm file1 && echo It worked! || echo It failed!
rm: cannot remove ?file1?: No such file or directory
It failed!
[email protected]:~$ rm file1 && echo It worked! || echo It failed!
rm: cannot remove ?file1?: No such file or directory
It failed!
[email protected]:~$

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 test2    # we create a directory
[email protected]:~$ cd test2      #### we enter the directory 
[email protected]:~/test2$ ls      # is it empty?
[email protected]:~/test2$

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]:~/test2$ echo Ram \; Shyam
Ram ; Shyam
[email protected]:~/test2$ echo Ram\ \ \ Shyam
Ram   Shyam
[email protected]:~/test2$ echo escaping \\\ \#\ \"\ \'
escaping \ # " '
[email protected]:~/test2$ echo escaping \\\?\*\"\'
escaping \?*"'
[email protected]:~/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.

[email protected]:~/test2$ echo Ram is \
> good \
> boy
Ram is good boy
[email protected]:~/test2$

another example of the end of line backslash

[email protected]:~/test2$ echo End of\
> line \
> backslash
End ofline backslash
[email protected]:~/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