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 />

This defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse a<br />

comma−separated data file. Note that $* uses the first character held in $IFS. See Example 5−1.<br />

bash$ echo $IFS | cat −vte<br />

$<br />

bash$ bash −c 'set w x y z; IFS=":−;"; echo "$*"'<br />

w:x:y:z<br />

$IFS does not handle whitespace the same as it does other characters.<br />

Example 9−1. $IFS and whitespace<br />

#!/bin/bash<br />

# $IFS treats whitespace differently than other characters.<br />

output_args_one_per_line()<br />

{<br />

for arg<br />

do echo "[$arg]"<br />

done<br />

}<br />

echo; echo "IFS=\" \""<br />

echo "−−−−−−−"<br />

IFS=" "<br />

var=" a b c "<br />

output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`<br />

#<br />

# [a]<br />

# [b]<br />

# [c]<br />

echo; echo "IFS=:"<br />

echo "−−−−−"<br />

IFS=:<br />

var=":a::b:c:::" # Same as above, but substitute ":" for " ".<br />

output_args_one_per_line $var<br />

#<br />

# []<br />

# [a]<br />

# []<br />

# [b]<br />

# [c]<br />

# []<br />

# []<br />

# []<br />

# The same thing happens with the "FS" field separator in awk.<br />

# Thank you, Stephane Chazelas.<br />

echo<br />

exit 0<br />

Chapter 9. Variables Revisited 66

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

Saved successfully!

Ooh no, something went wrong!