shell embedding and options
(Written by Paul Cobbaut, https://github.com/paulcobbaut/, with contributions by: Alex M. Schapelle, https://github.com/zero-pytagoras/)
This chapter takes a brief look at child shells
, embedded shells
and
shell options
.
shell embedding
Shells can be embedded
on the command line, or in other
words, the command line scan can spawn new processes containing a fork
of the current shell. You can use variables to prove that new shells are
created. In the screenshot below, the variable $var1 only exists in the
(temporary) sub shell.
[student@linux gen]$ echo $var1
[student@linux gen]$ echo $(var1=5;echo $var1)
5
[student@linux gen]$ echo $var1
[student@linux gen]$
You can embed a shell in an embedded shell
, this is
called nested embedding
of shells.
This screenshot shows an embedded shell inside an embedded shell.
student@linux:~$ A=shell
student@linux:~$ echo $C$B$A $(B=sub;echo $C$B$A; echo $(C=sub;echo $C$B$A))
shell subshell subsubshell
backticks
Single embedding can be useful to avoid changing your current directory.
The screenshot below uses backticks
instead of dollar-bracket to
embed.
[student@linux ~]$ echo `cd /etc; ls -d * | grep pass`
passwd passwd- passwd.OLD
[student@linux ~]$
You can only use the $()
notation to nest embedded
shells, backticks
cannot do this.
backticks or single quotes
Placing the embedding between backticks
uses one
character less than the dollar and parenthesis combo. Be careful
however, backticks are often confused with single quotes. The technical
difference between '
and `
is
significant!
[student@linux gen]$ echo `var1=5;echo $var1`
5
[student@linux gen]$ echo 'var1=5;echo $var1'
var1=5;echo $var1
[student@linux gen]$
shell options
Both set
and unset
are builtin shell
commands. They can be used to set options of the bash shell itself. The
next example will clarify this. By default, the shell will treat unset
variables as a variable having no value. By setting the -u option, the
shell will treat any reference to unset variables as an error. See the
man page of bash for more information.
[student@linux ~]$ echo $var123
[student@linux ~]$ set -u
[student@linux ~]$ echo $var123
-bash: var123: unbound variable
[student@linux ~]$ set +u
[student@linux ~]$ echo $var123
[student@linux ~]$
To list all the set options for your shell, use echo $-
.
The noclobber
(or -C
) option will be explained later in this book
(in the I/O redirection chapter).
[student@linux ~]$ echo $-
himBH
[student@linux ~]$ set -C ; set -u
[student@linux ~]$ echo $-
himuBCH
[student@linux ~]$ set +C ; set +u
[student@linux ~]$ echo $-
himBH
[student@linux ~]$
When typing set
without options, you get a list of all variables
without function when the shell is on posix
mode. You can set bash in
posix mode typing set -o posix
.
practice: shell embedding
1. Find the list of shell options in the man page of bash
. What is
the difference between set -u
and set -o nounset
?
2. Activate nounset
in your shell. Test that it shows an error
message when using non-existing variables.
3. Deactivate nounset.
4. Execute cd /var
and ls
in an embedded shell.
The echo
command is only needed to show the result of the ls
command. Omitting will result in the shell trying to execute the first
file as a command.
5. Create the variable embvar in an embedded shell and echo it. Does the variable exist in your current shell now ?
6. Explain what \"set -x\" does. Can this be useful ?
(optional)7. Given the following screenshot, add exactly four characters to that command line so that the total output is FirstMiddleLast.
[student@linux ~]$ echo First; echo Middle; echo Last
8. Display a long listing
(ls -l) of the passwd
command using the
which
command inside an embedded shell.
solution: shell embedding
1. Find the list of shell options in the man page of bash
. What is
the difference between set -u
and set -o nounset
?
read the manual of bash (man bash), search for nounset -- both mean the same thing.
2. Activate nounset
in your shell. Test that it shows an error
message when using non-existing variables.
set -u
OR
set -o nounset
Both these lines have the same effect.
3. Deactivate nounset.
set +u
OR
set +o nounset
4. Execute cd /var
and ls
in an embedded shell.
echo $(cd /var ; ls)
The echo
command is only needed to show the result of the ls
command. Omitting will result in the shell trying to execute the first
file as a command.
5. Create the variable embvar in an embedded shell and echo it. Does the variable exist in your current shell now ?
echo $(embvar=emb;echo $embvar) ; echo $embvar #the last echo fails
$embvar does not exist in your current shell
6. Explain what \"set -x\" does. Can this be useful ?
It displays shell expansion for troubleshooting your command.
(optional)7. Given the following screenshot, add exactly four characters to that command line so that the total output is FirstMiddleLast.
[student@linux ~]$ echo First; echo Middle; echo Last
echo -n First; echo -n Middle; echo Last
8. Display a long listing
(ls -l) of the passwd
command using the
which
command inside an embedded shell.
ls -l $(which passwd)