Chapter 15. shell embedding and options

Table of Contents

shell embedding
backticks
backticks or single quotes
shell options
practice: shell embedding
solution: shell embedding

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.

[paul@RHELv4u3 gen]$ echo $var1

[paul@RHELv4u3 gen]$ echo $(var1=5;echo $var1)
5
[paul@RHELv4u3 gen]$ echo $var1

[paul@RHELv4u3 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.

paul@deb503:~$ A=shell
paul@deb503:~$ 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.

[paul@RHELv4u3 ~]$ echo `cd /etc; ls -d * | grep pass`
passwd passwd- passwd.OLD
[paul@RHELv4u3 ~]$

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!

[paul@RHELv4u3 gen]$ echo `var1=5;echo $var1`
5
[paul@RHELv4u3 gen]$ echo 'var1=5;echo $var1'
var1=5;echo $var1
[paul@RHELv4u3 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.

[paul@RHEL4b ~]$ echo $var123

[paul@RHEL4b ~]$ set -u
[paul@RHEL4b ~]$ echo $var123
-bash: var123: unbound variable
[paul@RHEL4b ~]$ set +u
[paul@RHEL4b ~]$ echo $var123

[paul@RHEL4b ~]$

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

[paul@RHEL4b ~]$ echo $-
himBH
[paul@RHEL4b ~]$ set -C ; set -u
[paul@RHEL4b ~]$ echo $-
himuBCH
[paul@RHEL4b ~]$ set +C ; set +u
[paul@RHEL4b ~]$ echo $-
himBH
[paul@RHEL4b ~]$

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.

[paul@RHEL4b ~]$ 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.

[paul@RHEL4b ~]$ 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)