12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash</strong>-<strong>Scripting</strong> <strong>Guide</strong>#-------------------------------------------------------------------------# No space permitted on either side of = sign when initializing variables.# What happens if there is a space?# "VARIABLE =value"# ^#% Script tries to run "VARIABLE" command with one argument, "=value".# "VARIABLE= value"# ^#% Script tries to run "value" command with#+ the environmental variable "VARIABLE" set to "".#-------------------------------------------------------------------------echo hello # hello# Not a variable reference, just the string "hello" . . .echo $hello # 375# ^ This *is* a variable reference.echo ${hello} # 375# Also a variable reference, as above.# Quoting . . .echo "$hello" # 375echo "${hello}" # 375echohello="A B C D"echo $hello # A B C Decho "$hello" # A B C D# As you see, echo $hello and echo "$hello" give different results.# Why?# =======================================# Quoting a variable preserves whitespace.# =======================================echoecho '$hello' # $hello# ^ ^# Variable referencing disabled (escaped) by single quotes,#+ which causes the "$" to be interpreted literally.# Notice the effect of different types of quoting.hello= # Setting it to a null value.echo "\$hello (null value) = $hello"# Note that setting a variable to a null value is not the same as#+ unsetting it, although the end result is the same (see below).# --------------------------------------------------------------# It is permissible to set multiple variables on the same line,#+ if separated by white space.# Caution, this may reduce legibility, and may not be portable.var1=21 var2=22 var3=$V3echoecho "var1=$var1 var2=$var2 var3=$var3"Chapter 4. Introduction to Variables and Parameters 28

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

Saved successfully!

Ooh no, something went wrong!