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>#+ invalid variable assignmentvariable=\23skidooecho "$variable"# 23skidoo# This works, since the second line#+ is a valid variable assignment.variable=\# \^ escape followed by spaceecho "$variable" # spacevariable=\\echo "$variable" # \variable=\\\echo "$variable"# Will not work - gives an error message:# test.sh: \: command not found## First escape escapes second one, but the third one is left "naked",#+ with same result as first instance, above.variable=\\\\echo "$variable" # \\# Second and fourth escapes escaped.# This is o.k.Escaping a space can prevent word splitting in a command's argument list.file_list="/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7"# List of files as argument(s) to a command.# Add two files to the list, and list all.ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_listecho "-------------------------------------------------------------------------"# What happens if we escape a couple of spaces?ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list# Error: the first three files concatenated into a single argument to 'ls -l'# because the two escaped spaces prevent argument (word) splitting.The escape also provides a means of writing a multi-line command. Normally, each separate line constitutes adifferent command, but an escape at the end of a line escapes the newline character, and the commandsequence continues on to the next line.(cd /source/directory && tar cf - . ) | \(cd /dest/directory && tar xpvf -)# Repeating Alan Cox's directory tree copy command,# but split into two lines for increased legibility.# As an alternative:tar cf - -C /source/directory . |tar xpvf - -C /dest/directory# See note below.# (Thanks, Stéphane Chazelas.)If a script line ends with a |, a pipe character, then a \, an escape, is not strictly necessary. It is, however,good programming practice to always escape the end of a line of code that continues to the followingChapter 5. Quoting 42

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

Saved successfully!

Ooh no, something went wrong!