24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You are certainly familiar with straight parameter expansion, since it happens all the time, even in the<br />

simplest of cases, such as the one above or the following:<br />

franky ~> echo $SHELL<br />

/bin/bash<br />

The following is an example of indirect expansion:<br />

franky ~> echo ${!N*}<br />

NNTPPORT NNTPSERVER NPX_PLUGIN_PATH<br />

Note that this is not the same as echo $N*.<br />

The following construct allows for creation of the named variable if it does not yet exist:<br />

${VAR:=value}<br />

Example:<br />

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

franky ~> echo $FRANKY<br />

franky ~> echo ${FRANKY:=Franky}<br />

Franky<br />

Special parameters, among others the positional parameters, may not be assigned this way, however.<br />

We will further discuss the use of the curly braces for treatment of variables in Chapter 10. More information<br />

can also be found in the <strong>Bash</strong> info pages.<br />

3.4.5. Command substitution<br />

Command substitution allows the output of a command to replace the command itself. Command substitution<br />

occurs when a command is enclosed like this:<br />

$(command)<br />

or like this using backticks:<br />

`command`<br />

<strong>Bash</strong> performs the expansion by executing COMMAND and replacing the command substitution with the<br />

standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but<br />

they may be removed during word splitting.<br />

franky ~> echo `date`<br />

Thu Feb 6 10:06:20 CET 2003<br />

When the old-style backquoted form of substitution is used, backslash retains its literal meaning except when<br />

followed by "$", "`", or "\". The first backticks not preceded by a backslash terminates the command<br />

substitution. When using the "$(COMMAND)" form, all characters between the parentheses make up the<br />

command; none are treated specially.<br />

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backticks<br />

with backslashes.<br />

Chapter 3. The <strong>Bash</strong> environment 48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!