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.

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

v3=${v0/#123/000}<br />

echo "v3 = $v3"<br />

v4=${v0/%123/000}<br />

echo "v4 = $v4"<br />

# Matches, but not at beginning.<br />

# abc1234zip1234abc<br />

# NO REPLACEMENT.<br />

# Matches, but not at end.<br />

# abc1234zip1234abc<br />

# NO REPLACEMENT.<br />

exit 0<br />

${!varprefix*}, ${!varprefix@}<br />

Matches all previously declared variables beginning with varprefix.<br />

xyz23=whatever<br />

xyz24=<br />

a=${!xyz*}<br />

echo "a = $a"<br />

a=${!xyz@}<br />

echo "a = $a"<br />

# Expands to names of declared variables beginning with "xyz".<br />

# a = xyz23 xyz24<br />

# Same as above.<br />

# a = xyz23 xyz24<br />

# Bash, version 2.04, adds this feature.<br />

9.4. Typing variables: declare or typeset<br />

The declare or typeset builtins (they are exact synonyms) permit restricting the properties of variables. This<br />

is a very weak form of the typing available in certain programming languages. The declare command is<br />

specific to version 2 or later of Bash. The typeset command also works in ksh scripts.<br />

declare/typeset options<br />

−r readonly<br />

declare −r var1<br />

(declare −r var1 works the same as readonly var1)<br />

This is the rough equivalent of the C const type qualifier. An attempt to change the value of a<br />

readonly variable fails with an error message.<br />

−i integer<br />

declare −i number<br />

# The script will treat subsequent occurrences of "number" as an integer.<br />

number=3<br />

echo "number = $number" # number = 3<br />

number=three<br />

echo "number = $number" # number = 0<br />

# Tries to evaluate "three" as an integer.<br />

Note that certain arithmetic operations are permitted for declared integer variables without the need<br />

for expr or let.<br />

−a array<br />

declare −a indices<br />

Chapter 9. Variables Revisited 92

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

Saved successfully!

Ooh no, something went wrong!