Unix Shell - quoting in bash

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2006 > quoting in bash





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author quoting in bash
Charles Russell

2006-08-23, 1:26 pm

Why does ls see something different from echo?

$ ls "dumb win"
../ ../ go_linux

$ QWIN=\""dumb win"\"

$ echo $QWIN
"dumb win"

$ ls $QWIN
ls: "dumb: No such file or directory
ls: win": No such file or directory
Stephane CHAZELAS

2006-08-23, 1:26 pm

2006-08-23, 12:27(-05), Charles Russell:
> Why does ls see something different from echo?
>
> $ ls "dumb win"
> ./ ../ go_linux
>
> $ QWIN=\""dumb win"\"
>
> $ echo $QWIN
> "dumb win"
>
> $ ls $QWIN
> ls: "dumb: No such file or directory
> ls: win": No such file or directory


What makes you think it sees something different?

echo also sees two arguments `"dumb"' and `win"' which it
displays separated by one space.

You want:

QWIN='dumb win'
ls -- "$QWIN"

--
Stéphane
Chris Mattern

2006-08-23, 7:27 pm

Charles Russell wrote:
> Why does ls see something different from echo?
>
> $ ls "dumb win"
> ./ ../ go_linux
>
> $ QWIN=\""dumb win"\"
>
> $ echo $QWIN
> "dumb win"
>
> $ ls $QWIN
> ls: "dumb: No such file or directory
> ls: win": No such file or directory


It doesn't.

$ echo "dumb win"
dumb win

And try this:

$ QWIN=\""dumb win"\"
$ echo $QWIN
"dumb win"


What happens is the shell parsing interprets quotes *before*
it does variable substitution. So instead of ls or echo being
passed one argument with no quotes, they get two arguments
with the quotes left in.


Chris Mattern
Charles Russell

2006-08-23, 7:27 pm

Stephane CHAZELAS wrote:
>
> You want:
>
> QWIN='dumb win'
> ls -- "$QWIN"
>


Well, what I really want is a way to avoid typing quotes every time I
reference QWIN, but I guess that is impossible.
Charles Russell

2006-08-23, 7:27 pm

Chris Mattern wrote:

>
> And try this:
>
> $ QWIN=\""dumb win"\"
> $ echo $QWIN
> "dumb win"
>


Nice example.
Stachu 'Dozzie' K.

2006-08-23, 7:27 pm

On 23.08.2006, Charles Russell <SPAMworFREEwor@bellsouth.net> wrote:
> Stephane CHAZELAS wrote:
>
> Well, what I really want is a way to avoid typing quotes every time I
> reference QWIN, but I guess that is impossible.


It is possible. Change shell to zsh and set no_sh_word_split option.

--
<Kosma> Niektórzy lubi± dozziego...
<Kosma> Oczywi¶cie szanujemy ich.
Stanislaw Klekot
Jon LaBadie

2006-08-23, 7:27 pm

Charles Russell wrote:
> Stephane CHAZELAS wrote:
>
> Well, what I really want is a way to avoid typing quotes every time I
> reference QWIN, but I guess that is impossible.


You are hard to convince.

I thought that was the same general conclusion of your
previous thread "quoting pathnames with spaces"?
Charles Russell

2006-08-23, 7:27 pm

Jon LaBadie wrote:
> Charles Russell wrote:
>
>
>
> You are hard to convince.
>
> I thought that was the same general conclusion of your
> previous thread "quoting pathnames with spaces"?


Yes, but as you can tell, I don't understand exactly how the quoting
works, and it looked - from echo - like it ought to do what I wanted.
Charles Russell

2006-08-23, 7:27 pm

Stachu 'Dozzie' K. wrote:
> On 23.08.2006, Charles Russell <SPAMworFREEwor@bellsouth.net> wrote:
>
>
>
> It is possible. Change shell to zsh and set no_sh_word_split option.
>


Thanks. The suggestion is useful. I looked at zsh on wikipedia to see if
it had other features that I could use, but since I just use the shell
to hack simple utilities, I don't need all that power. Though I hate
typing those damned quotes over and over again, the problem is not great
enough to justify switching shells. There are simple workarounds, using
aliases or symbolic links.
bsh

2006-08-24, 1:27 am

Charles Russell wrote:
> Why does ls see something different from echo?


In general:

"A Guide to Unix Shell Quoting"
http://www.mpi-sb.mpg.de/~uwe/lehre...ting-guide.html

"Consultix Shell Quoting Guidelines"
http://www.consultix-inc.com/quoting1_2.txt

"UNIX SHELL Quote Tutorial"
http://www.grymoire.com/Unix/Quote.html

"The Open Group Base Specifications (IEEE Std 1003.1) -- 2.6 Word
Expansions"
http://www.opengroup.org/onlinepubs....html#tag_02_06

I've arrange them in order of general applicability,
accessibility, and completeness. The fact that there's
more than one is in accordance to the degree of how
prone the topic is to misunderstanding and error!

=Brian

Divakar

2006-08-24, 1:27 am

Charles Russell wrote the following on 8/23/2006 10:57 PM:
> Why does ls see something different from echo?
>
> $ ls "dumb win"
> ./ ../ go_linux
>
> $ QWIN=\""dumb win"\"
>
> $ echo $QWIN
> "dumb win"
>
> $ ls $QWIN
> ls: "dumb: No such file or directory
> ls: win": No such file or directory



How about changing IFS..?

<bash > QWIN=\""dumb win"\"
<bash > IFS=":"
<bash > ls $QWIN
ls: "dumb win": No such file or directory
<bash > IFS=" "
<bash > ls $QWIN
ls: "dumb: No such file or directory
ls: win": No such file or directory

--divakar
Charles Russell

2006-08-24, 7:26 pm

bsh wrote:
> Charles Russell wrote:
>
>
>
> In general:
>
> "A Guide to Unix Shell Quoting"
> http://www.mpi-sb.mpg.de/~uwe/lehre...ting-guide.html
>
> "Consultix Shell Quoting Guidelines"
> http://www.consultix-inc.com/quoting1_2.txt
>
> "UNIX SHELL Quote Tutorial"
> http://www.grymoire.com/Unix/Quote.html
>
> "The Open Group Base Specifications (IEEE Std 1003.1) -- 2.6 Word
> Expansions"
> http://www.opengroup.org/onlinepubs....html#tag_02_06
>
> I've arrange them in order of general applicability,
> accessibility, and completeness. The fact that there's
> more than one is in accordance to the degree of how
> prone the topic is to misunderstanding and error!
>
> =Brian
>

Thanks. I had already found the first of these, but it was too
forbidding for me to tackle. The second reference on your list
describes its audience as advanced end-user on up. I'm not a
programmer; I just crunch numbers with fortran, and probably fit in the
"advanced end-user" category. That reference,and the third one, are
more on my level.
Charles Russell

2006-08-24, 7:26 pm

Divakar wrote:
> Charles Russell wrote the following on 8/23/2006 10:57 PM:
>
>
>
>
> How about changing IFS..?
>
> <bash > QWIN=\""dumb win"\"
> <bash > IFS=":"
> <bash > ls $QWIN
> ls: "dumb win": No such file or directory
> <bash > IFS=" "
> <bash > ls $QWIN
> ls: "dumb: No such file or directory
> ls: win": No such file or directory
>
> --divakar


OK in a script, but at the command line, harder than typing quotes, and
much easier to get wrong. I was looking for a macro that is quick and
easy to use at the command line and can be used flexibly in a variety of
commands. Apparently not possible in Bourne or even bash.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com