Quoting, Control Statements
Quoting
Double Quotes
● Double quotes tell the shell to not interpret the special characters (meta characters).
Meta characters are symbols that have special meaning to the shell ( *, ?, space, and etc ).
● Double quotations are used display or use a value that is a special character to the shell.
● Double quotes allow the user of command substitution, variable substitution.
ivanovn@atlas:~$ var=hello there
Command 'there' not found, did you mean:
command 'theme' from deb discount (2.2.6-1)
Try: apt install <deb name>
● In the above example, the space divided the “var=hello threre” statement into two parts:
● var=hello
● there (this is not a valid statement and, as a result, a syntax error was shown
● The double quotations can be used to fix the above statement:
var=“hello there”
Single Quotes
● Single quotes prevent the shell from doing any interpreting of special characters,
including globs, variables, command substitution and other metacharacters.
ivanovn@atlas:~$ var=hello
ivanovn@atlas:~$ echo '$var'
$var
Backslash Character
● A backslash character is an escape character – it takes the special meaning away from meta
characters and other special characters.
ivanovn@atlas:~$ var=hello
ivanovn@atlas:~$ echo $var
hello
ivanovn@atlas:~$ echo \$var
$var
ivanovn@atlas:~$ echo *
Desktop Documents Downloads Music Pictures Public Templates Videos
ivanovn@atlas:~$ echo \*
*
ivanovn@atlas:~$ var=hello\ there
Backquotes
● The command substitution is an ability to run a command and use the output of that
command.
ivanovn@atlas:~$ echo Today is date
Today is date
● To execute the date command so the output of the date command is sent to
the echo command, put the date command inside of two backquotes:
ivanovn@atlas:~$ echo Today is `date`
Today is Fri Dec 14 01:00:04 UTC 2000
● Alternatively, the command substition can be done by placing the command inside of
$()
ivanovn@atlas:~$ echo Today is $(date)
Today is Fri Dec 14 01:00:04 UTC 2000
Control Statements
Control Statements
● Control statements allow you to run multiple commands from the same command line
● The following control statements can be used:
○ Semicolon (;)
○ Double ampersand (&&)
○ Double pipe (||)
Control Statements
● The semicolon can be used to run multiple commands, one after the other
● The next command runs even if the previous command fails
● cal ; date
● With the double ampersand (logical AND) &&, the next command is executed only if previous
command succeeds
● cal && date
● With the double pipe (logical OR) ||, the next command runs only if the previous command fails
● cal || date
● Examples:
● ls /home && echo success || echo failure
● ls /root && echo success || echo failure
● The above examples will print “success” if the ls command succeeds and will print “failure” if the ls
command fails