[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: $(grep anycommand anyfile) fail
From: |
Dave B |
Subject: |
Re: $(grep anycommand anyfile) fail |
Date: |
Wed, 24 Dec 2008 16:57:53 +0100 |
User-agent: |
Thunderbird 2.0.0.18 (X11/20081124) |
BlackEnvil wrote:
> Description: using ` ` or $() with command that use dirnames with spaces
> fail.
>
> there are diferent dirnames with this problem, and different situations that
> cause these errors, not only with ls and not only with grep.
>
> bye
>
>
> Repeat-By:
>
> [blackenvil@space_star ~]$ cd $HOME; mkdir hello\ -world/; touch test; echo
> "ls hello\ -world/" > test; $(grep ls test);
> ls: invalid line width: orld/
> [blackenvil@space_star ~]$
There are actually two problems here.
After the command substitution, the shell does word splitting, and the
command it sees is this (spaced for readability):
ls hello\ -world
^^^^^^ ^^^^^^
arg1 arg2
The -world part is interpreted as the option -w to ls, and "orld" as the
argument to that option, which is an invalid line width. To fix that, use --
to mark the end of options. But even if you do that, you would still get an
error because ls would see two filenames: "hello\" and "-world", neither of
which exists.
> [blackenvil@space_star ~]$ cd $HOME; mkdir hello-\ world/; touch test; echo
> "ls hello-\ world/" > test; $(grep ls test);
> ls: cannot access hello-\: No such file or directory
> world/:
> [blackenvil@space_star ~]$
Same as above: the shell does word splitting on the result of command
substitution, and what it sees is the command (spaced for readability)
ls hello-\ world
^^^^^^^ ^^^^^
arg1 arg2
since those files or directory do not exist, ls correctly produces an error.
In both cases what you're seeing is not a bug, but just expected behavior.
--
D.