12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

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</strong>-<strong>Scripting</strong> <strong>Guide</strong>d=${c/BB/23}# Substitute "23" for "BB".# This makes $d an integer.echo "d = $d" # d = 2334let "d += 1" # 2334 + 1 =echo "d = $d" # d = 2335echo# What about null variables?e=""echo "e = $e" # e =let "e += 1"# Arithmetic operations allowed on a null variable?echo "e = $e" # e = 1echo# Null variable transformed into an integer.# What about undeclared variables?echo "f = $f" # f =let "f += 1"# Arithmetic operations allowed?echo "f = $f" # f = 1echo# Undeclared variable transformed into an integer.# Variables in <strong>Bash</strong> are essentially untyped.exit 0Untyped variables are both a blessing and a curse. They permit more flexibility in scripting (enough rope tohang yourself!) and make it easier to grind out lines of code. However, they permit errors to creep in andencourage sloppy programming habits.To lighten the burden of keeping track of variable types in a script, <strong>Bash</strong> does permit declaring variables.4.4. Special Variable Typeslocal variablesvariables visible only within a code block or function (see also local variables in functions)environmental variablesvariables that affect the behavior of the shell and user interfaceIn a more general context, each process has an "environment", that is, a group ofvariables that hold information that the process may reference. In this sense, the shellbehaves like any other process.Every time a shell starts, it creates shell variables that correspond to its ownenvironmental variables. Updating or adding new environmental variables causes theshell to update its environment, and all the shell's child processes (the commands itexecutes) inherit this environment.The space allotted to the environment is limited. Creating too many environmentalvariables or ones that use up excessive space may cause problems.bash$ eval "`seq 10000 | sed -e 's/.*/export var&=ZZZZZZZZZZZZZZ/'`"bash$ dubash: /usr/bin/du: Argument list too longChapter 4. Introduction to Variables and Parameters 32

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

Saved successfully!

Ooh no, something went wrong!