Bash: Preserving Whitespace Using set and eval
If you don't care much about whitespace bash is great: it normally turns multiple whitespace characters into one and it breaks things into words based on white space. If on the other hand you'd like to preserve whitespace bash can be a bit difficult at times. A trick which often helps is using a combination of bash's eval and set commands.
Let's say that you're building a list of items where each item may contain significant spaces, say something like:
#!/bin/bash
items=
for i in "$@"
do
items="$items \"$i\""
done
for i in $items
do
echo $i
done
But when you run this and try to use the items from the saved list you don't quite get what you expected:
$ sh t1.sh "ab cd" "ef gh" "ab cd" "ef gh"
One solution is to do the following:
#!/bin/bash
items=
for i in "$@"
do
items="$items \"$i\""
done
eval set -- $items
for i in "$@"
do
echo $i
done
Which produces the desired result:
$ sh t2.sh "ab cd" "ef gh" ab cd ef gh
The important line is:
eval set -- $items
The set command takes any arguments after the options (here "--" signals the end of the options) and assigns them to the positional parameters ($0..$n). The eval command executes its arguments as a bash command.
If you do this without the eval command you'll get the same result as the first example. By passing the set command to eval bash will honor the embedded quotes in the string rather than assume they are part of the word.
If you run this script you can see a bit more of what bash is doing:
#!/bin/bash
items=
for i in "$@"
do
items="$items \"$i\""
done
set -x
set -- $items
set +x
echo '===='
set -x
eval set -- $items
set +x
This produces:
$ sh t3.sh "ab cd" "ef gh" + set -- '"ab' 'cd"' '"ef' 'gh"' + set +x ==== + eval set -- '"ab' 'cd"' '"ef' 'gh"' ++ set -- 'ab cd' 'ef gh' + set +x
Mitch Frazier is an Associate Editor for Linux Journal.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android
- Employment Posters
3 hours 48 min ago - Sure the best distro is
5 hours 8 min ago - BeOS was the best
7 hours 51 min ago - I use Wireshark on a daily
12 hours 22 min ago - buena información
17 hours 29 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
18 hours 29 min ago - Gnome3 is such a POS. No one
1 day 3 hours ago - Gnome 3 is the biggest POS
1 day 4 hours ago - I didn't knew this thing by
1 day 10 hours ago - Author's reply
1 day 13 hours ago





Comments
I dont think I am quite getting eval with arrays!
How about pulling lines from a file into an array while retaining whitespaces on each line? For example:
___________________________________________________________________
#releases.txt
#
CentOS release 5.2 (Final)
CentOS release 5.3 (Final)
CentOS release 5.4 (Final)
Fedora Core release 4 (Stentz)
Fedora Core release 5 (Bordeaux)
Fedora Core release 6 (Zod)
Fedora release 7 (Moonshine)
Fedora release 8 (Werewolf)
Fedora release 9 (Sulphur)
Red Hat Enterprise Linux AS release 4 (Nahant Update 2)
Red Hat Enterprise Linux ES release 3 (Taroon Update 1)
Red Hat Linux release 8.0 (Psyche)
Red Hat Linux release 9 (Shrike)
Ubuntu 2.6.22-14.46-server
Ubuntu 2.6.31-15.50-generic
____________________________________________________________________
so if your array was MYARRAY and the lines where as shown above then $MYARRAY[0] would equal: CentOS release 5.2 (Final)
Would eval work for this or am I barking up the wrong tree?
I guess technically in that
I guess technically in that little file $MYARRAY[0] would equal "#releases.txt" and $MYARRAY[2] would equal: CentOS release 5.2 (Final)
But i think you can get what I am trying to say!
DANGER!!! the eval executes the arguments
try this:
t1.sh "ab cd" "ef \$(pwd) gh"
or
t1.sh "ab cd" 'ef $(pwd) gh'
What about non-quoted?
The IFS seems to fix the multiple space problem:
------------------------
#!/bin/bash
IFS=
while read line
do
echo $line
done
------------------------
This also does not need quoted input.
On the example of reading from a file
Suppose the user types
"ab"
"a b"
"a b"
"a b"
for stdin. We get:
ab
a b
a b
a b
I.e., multiple spaces are reduced to one space.
Embedded Quotes
The example given has problems with inputs that contain quotes
$ t1.sh "foo says, \"bar\""gives
foo says, barTo fix this, the inherent quote in each element needs to be replaced. The resulting script:
#!/bin/bash
items=
for i in "$@"
do
items="$items \"$(echo $i | sed 's/"/\\"/g')\""
done
eval set -- $items
for i in "$@"
do
echo $i
done
gives the desired result:
foo says, "bar"Simple way
A simple way would be to use an array :
items=("$@") for i in "${items[@]}"; do echo $i donexavier
For this example
Using an array is certainly an option in the example I used, but suppose the data with significant spaces is being read from a file or from the terminal such as in this example:
If the user types "ab cd" "ef gh" and we'd like to display ab cd and ef gh (without quotes) then you need eval. Another alternative that does use arrays is the following:
Mitch Frazier is an Associate Editor for Linux Journal.
Thanks for clarifying that
Thanks for clarifying that part!