[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Characters aren't being properly escaped/evaluated in a Bash
From: |
Paul Jarc |
Subject: |
Re: Characters aren't being properly escaped/evaluated in a Bash |
Date: |
Thu, 10 May 2007 14:37:09 -0400 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.4 (gnu/linux) |
garrett.w.cooper@intel.com wrote:
> The problem is that if I did this out of the box
> like the following, it 'flattens' the list:
>
> Example:
> for j in `cat $i`; do
> echo $j
> done
>
> Output:
> This
> is
> a
> file
> [...]
Inserting quotes into the output of the command substitution won't
help. Quotes are given special syntactic status only if they are
unquoted themselves, and do not appear as the result of any kind of
expansion.
You can use a "while read" loop as Chet suggested, or you can tell
bash to split the `cat` expansion only at newlines, not as other
whitespace:
old_ifs=$IFS
IFS=$'\n'
for j in `cat "$i"`; do
...
done
IFS=$old_ifs
If you have other unquoted expansions within the body of the for loop
that should use the normal IFS for splitting, then you'll have to
restore the original IFS after splitting `cat`, but before entering the
for loop:
old_ifs=$IFS
IFS=$'\n'
set x `cat "$i"`
shift
IFS=$old_ifs
for j in "$@"; do
...
done
paul