In article <slrnkaslia.7...@wb.home.test>,
Bit Twister <
BitTw...@mouse-potato.com> wrote:
>I am trying to use a variable in an alias for xterm.
>
>$ alias xterm=`xterm -title \"$PWD\"'
>$ xterm which works just fine.
Didn't you notice that the quotation marks also show up in the title? That's
a clue. Quoting $PWD was a smart thing to do, since it will prevent
word-splitting when the directory name contains a space.
But backslashing the quotation marks undid that. The whole alias is quoted by
the outer apostrophes (after correcting the backtick typo) so everything
inside becomes part of the command, including the quotation marks and the
backslashes. When the command runs, $PWD is no longer quoted, so it gets
split, and the quotation marks are just literally added to the front and back
of it.
You only think it worked because you didn't test it in a directory with
spaces in its name.
[...]
>Here is my latest attempt at it. echo is in there for
>debugging. Watch out for line wrap.
>
>$ alias xterm='dt=$(qdbus org.kde.kwin /KWin currentDesktop) ; echo
>/usr/bin/xterm -title \"$PWD dt=$dt\"'
The problem here is the same. Those quotation marks are not active because of
the backslashes.
>
>Now the command "xterm" gives
> /usr/bin/xterm -title "/home/bittwister/txt dt=3"
>which is correct.
No, it's not correct. Think harder. Look at this:
(1) $ echo hello > 'foo bar'
(2) $ echo oops > foo
(3) $ echo oops2 > bar
(4) $ file='foo bar'
(5) $ cat $file
oops
oops2
(6) $ cat "$file"
hello
(7) $ echo cat $file
cat foo bar
(8) $ echo cat "$file"
cat foo bar
(9) $ echo cat \"$file\"
cat "foo bar"
(10) $ cat \"$file\"
cat: "foo: No such file or directory
cat: bar": No such file or directory
I've created a file with a space in its name, put the name in a variable, and
attempted to cat it two different ways. The incorrect way, command number
(5), lacks quoting so it splits the variable and cats the wrong files. The
correct way, with quoting, is (6).
(7) is the incorrect command, same as (5), with an echo stuck on the front.
(8) is the incorrect command, same as (6), with an echo stuck on the front.
Notice that (7) and (8) both output the same thing, which looks like the
incorrect cat command. echo doesn't distinguish between two separate args and
a single arg that contains a space. By using echo to inspect a command before
running it, you lose information about which words were quoted.
Conversely, (9) is an echo command that displays the correct cat command. But
as shown in (10), it doesn't become a correct cat command just by stripping
away the echo. (It could become correct with some help from eval, but that's
overkill for a problem this simple.)
zsh has a cool feature that's useful for echo inspections without losing
quoting information:
% file='foo bar'
% echo $file
foo bar
% echo ${(qq)file}
'foo bar'
You could make a helper script to echo an entire command with quotes:
$ cat echoquoted
#!/usr/bin/zsh -f
echo ${(qq)@}
$ file='foo bar'
$ ./echoquoted cat $file
'cat' 'foo' 'bar'
$ ./echoquoted cat "$file"
'cat' 'foo bar'
$ ./echoquoted cat \"$file\"
'cat' '"foo' 'bar"'
Using echoquoted instead of echo, both incorrect versions of the command are
distinguishable from the correct version.
--
Alan Curry