27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

# Piping an empty "echo" to awk gives it dummy input,<br />

#+ and thus makes it unnecessary to supply a filename.<br />

exit 0<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

9.2.2. Further Discussion<br />

For more on string manipulation in scripts, refer to Section 9.3 and the relevant section of the expr command<br />

listing. For script examples, see:<br />

1. Example 12−6<br />

2. Example 9−15<br />

3. Example 9−16<br />

4. Example 9−17<br />

5. Example 9−19<br />

9.3. Parameter Substitution<br />

Manipulating and/or expanding variables<br />

${parameter}<br />

Same as $parameter, i.e., value of the variable parameter. In certain contexts, only the less<br />

ambiguous ${parameter} form works.<br />

May be used for concatenating variables with strings.<br />

your_id=${USER}−on−${HOSTNAME}<br />

echo "$your_id"<br />

#<br />

echo "Old \$PATH = $PATH"<br />

PATH=${PATH}:/opt/bin #Add /opt/bin to $PATH for duration of script.<br />

echo "New \$PATH = $PATH"<br />

${parameter−default}, ${parameter:−default}<br />

If parameter not set, use default.<br />

echo ${username−`whoami`}<br />

# Echoes the result of `whoami`, if variable $username is still unset.<br />

${parameter−default} and ${parameter:−default} are almost<br />

equivalent. The extra : makes a difference only when parameter has been declared,<br />

but is null.<br />

#!/bin/bash<br />

username0=<br />

# username0 has been declared, but is set to null.<br />

echo "username0 = ${username0−`whoami`}"<br />

# Will not echo.<br />

echo "username1 = ${username1−`whoami`}"<br />

# username1 has not been declared.<br />

# Will echo.<br />

Chapter 9. Variables Revisited 84

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

Saved successfully!

Ooh no, something went wrong!