Re: Bash problem with subshell and *
On Wed, 13 Dec 2006 12:10:58 +0100, Andrea Ganduglia wrote:
> Hi. I'm working on Sarge. I'm parsing a text file when..
>
> cd tmp
> ls
> aaa bbb ccc ddd
>
> My script parse all file into directory, and grep ^Subject line.
>
> for i in *; do
> egrep '^Subject:' $i
> done
>
> Subject: Hello Andrea
> Subject: Ciao Debiam
> Subject: {SpAm?} * Viiagrra * Ciialiis * Leevittra *
> Subject: Good OS
>
> Ok? But if I modify my script and store egrep result into VAR
>
> for i in *; do
> SUBJECT=$(egrep '^Subject:' $i)
> echo $SUBJECT
> done
>
> Subject: Hello Andrea
> Subject: Ciao Debiam
> aaa bbb ccc ddd
> Subject: Good OS
>
> In other words subshell expand willcard `*' and shows all files into
> directory!
You need to put double quotes around the variable when echoing it:
echo "$SUBJECT"
This will prevent pathname expansion (which normally takes place
_after_ variable expansion). echo by itself does not disable the
normal processing of the command line, which includes pathname
expansion.
In case you'd like to understand all the nitty-gritty details, the bash
manpage will provide lots of reading material ;) For example, you'll
find statements such as
EXPANSION
Expansion is performed on the command line after it has been
split into words. There are seven kinds of expansion performed:
brace expansion, tilde expansion, parameter and variable
expansion, command substitution, arithmetic expansion, word
splitting, and pathname expansion.
QUOTING
(...)
Enclosing characters in double quotes preserves the literal
value of all characters within the quotes, with the exception
of $, `, \, and, when history expansion is enabled, !.
Admittedly though, it needs careful reading, and sometimes a bit of
reading between the lines...
Cheers,
Almut
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
Reply to: