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.

Chapter 35. Bash, version 2<br />

The current version of Bash, the one you have running on your machine, is actually version 2.XX.Y.<br />

bash$ echo $BASH_VERSION<br />

2.05.8(1)−release<br />

This update of the classic Bash scripting language added array variables, [66] string and parameter expansion,<br />

and a better method of indirect variable references, among other features.<br />

Example 35−1. String expansion<br />

#!/bin/bash<br />

# String expansion.<br />

# Introduced with version 2 of Bash.<br />

# Strings of the form $'xxx'<br />

# have the standard escaped characters interpreted.<br />

echo $'Ringing bell 3 times \a \a \a'<br />

echo $'Three form feeds \f \f \f'<br />

echo $'10 newlines \n\n\n\n\n\n\n\n\n\n'<br />

exit 0<br />

Example 35−2. Indirect variable references − the new way<br />

#!/bin/bash<br />

# Indirect variable referencing.<br />

# This has a few of the attributes of references in C++.<br />

a=letter_of_alphabet<br />

letter_of_alphabet=z<br />

echo "a = $a"<br />

# Direct reference.<br />

echo "Now a = ${!a}" # Indirect reference.<br />

# The ${!variable} notation is greatly superior to the old "eval var1=\$$var2"<br />

echo<br />

t=table_cell_3<br />

table_cell_3=24<br />

echo "t = ${!t}" # t = 24<br />

table_cell_3=387<br />

echo "Value of t changed to ${!t}" # 387<br />

# This is useful for referencing members of an array or table,<br />

# or for simulating a multi−dimensional array.<br />

# An indexing option would have been nice (sigh).<br />

Chapter 35. Bash, version 2 355

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

Saved successfully!

Ooh no, something went wrong!