;
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.
[root@redhat
~]$ echo Hello
Hello
[root@redhat
~]$ echo World
World
[root@redhat
~]$ echo Hello ; echo World
Hello
World
[root@redhat ~]$
&
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
background.
You will get a message when this command has finished executing in
background.
[root@redhat
~]$ sleep 20 &
[1] 7925
[root@redhat
~]$
...wait 20
seconds...
[root@redhat
~]$
[1]+ Done sleep 20
&&
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).
root@redhat:~$
echo first && echo second
first
second
root@redhat:~$
zecho first && echo second
-bash:
zecho: command not found
Another
example of the same logical AND principle. This example starts with a
working cd
followed by ls, then a non-working cd which is not followed
by ls.
[root@redhat
~]$ cd gen && ls
file1
file3 File55 fileab FileAB fileabc
file2
File4 FileA Fileab fileab2
[root@redhat
gen]$ 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).
root@redhat:~$
echo first || echo second ; echo third
first
third
root@redhat:~$
zecho first || echo second ; echo third
-bash:
zecho: command not found
second
third
root@redhat:~$
Another
example of the same logical OR principle.
[root@redhat
~]$ cd gen || ls
[root@redhat
gen]$ cd gen || ls
-bash: cd:
gen: No such file or directory
file1
file3 File55 fileab FileAB fileabc
file2 File4 FileA Fileab fileab2
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.
root@redhat:~/test$
rm file1 && echo It worked! || echo It failed!
It worked!
root@redhat:~/test$
rm file1 && echo It worked! || echo It failed!
rm: cannot
remove `file1': No such file or directory
It failed!
root@redhat:~/test$
#
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.
root@redhat:~$
mkdir test # we create a directory
root@redhat:~$
cd test #### we enter the directory
root@redhat:~/test$
ls # is it empty ?
root@redhat:~/test$
\
escaping special characters
The
backslash \ character enables the use of control characters, but without
the shell
interpreting
it, this is called escaping characters.
[root@redhat
~]$ echo hello \; world
hello ;
world
[root@redhat
~]$ echo hello\ \ \ world
hello
world
[root@redhat
~]$ echo escaping \\\ \#\ \&\ \"\ \'
escaping \
# & " '
[root@redhat
~]$ echo escaping \\\?\*\"\'
escaping \?*"'
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@redhat
~]$ echo This command line \
> is
split in three \
> parts
This
command line is split in three parts
[root@redhat ~]$
No comments:
Post a Comment