Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I use a variable in xterm alias

193 views
Skip to first unread message

Bit Twister

unread,
Nov 22, 2012, 11:43:54 AM11/22/12
to
I am trying to use a variable in an alias for xterm.

$ alias xterm=`xterm -title \"$PWD\"'
$ xterm which works just fine.

Now I want $PWD and which desktop xterm is running in.

Tried several methods and they keep coming up with
"No absolute path found for shell: dt=5"

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\"'

Now the command "xterm" gives
/usr/bin/xterm -title "/home/bittwister/txt dt=3"
which is correct.

Doing an up arrow and removing echo from the alias, I get,
$ xterm
/usr/bin/xterm: No absolute path found for shell: dt=3"

I would like to do it without a script or function.

Any suggestions welcome.

I am going to go mow my yard leaves to relieve the stress from trying
to get such a simple line of code to work and failing.

Chris F.A. Johnson

unread,
Nov 22, 2012, 12:17:46 PM11/22/12
to
On 2012-11-22, Bit Twister wrote:
> I am trying to use a variable in an alias for xterm.
>
> $ alias xterm=`xterm -title \"$PWD\"'

Why do you have an unmatched backtick?

man bash:

For almost every purpose, aliases are superseded by shell functions.

...
> I would like to do it without a script or function.

Why? Why not use the better way?

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Bit Twister

unread,
Nov 22, 2012, 12:54:37 PM11/22/12
to
On Thu, 22 Nov 2012 12:17:46 -0500, Chris F.A. Johnson wrote:
> On 2012-11-22, Bit Twister wrote:
>> I am trying to use a variable in an alias for xterm.
>>
>> $ alias xterm=`xterm -title \"$PWD\"'
>
> Why do you have an unmatched backtick?

Probably from me hacking the terminal stuff into the post. Sorry, My bad :(
actual bashrc has no backticks, just ticks :)


>> I would like to do it without a script or function.
>
> Why? Why not use the better way?

"alias xterm" when defined as an alias shows the definition :-D
"alias xterm" when defined as a function, does not. :-(

Yes, I could use "type xterm" instead.

I still would like to know how to get a variable displayed within -title.

Dave Gibson

unread,
Nov 22, 2012, 2:28:35 PM11/22/12
to
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.

It works by accident. If you try it with a variable containing spaces
you'll see what's going wrong.

>
> Now I want $PWD and which desktop xterm is running in.
>
> Tried several methods and they keep coming up with
> "No absolute path found for shell: dt=5"

The real error message is something like:

/usr/bin/xterm: No absolute path found for shell: dt=5"

The double quote on the end is the giveaway.

>
> 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\"'
>
> Now the command "xterm" gives
> /usr/bin/xterm -title "/home/bittwister/txt dt=3"
> which is correct.

Use printf for a clearer view:

$ alias xt='dt=3 ; printf %s\\n /usr/bin/xterm -title \"$PWD dt=$dt\"'
$ xt
/usr/bin/xterm
-title
"/home/dave
dt=3"

The alias expands to

dt=3 ; printf %s\\n /usr/bin/xterm -title \"$PWD dt=$dt\"

>
> Doing an up arrow and removing echo from the alias, I get,
> $ xterm
> /usr/bin/xterm: No absolute path found for shell: dt=3"
>
> I would like to do it without a script or function.
>
> Any suggestions welcome.

You don't need to quote double-quotes within single-quotes.

$ alias xterm=' ... ; /usr/bin/xterm -title "$PWD dt=$dt"'

Alan Curry

unread,
Nov 22, 2012, 3:14:48 PM11/22/12
to
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

Chris F.A. Johnson

unread,
Nov 22, 2012, 3:38:22 PM11/22/12
to
Or in any shell:

$ cat eq

printf "'%s' " "${0##*/}" "$@"
echo

Bit Twister

unread,
Nov 22, 2012, 4:07:09 PM11/22/12
to
On Thu, 22 Nov 2012 19:28:35 +0000, Dave Gibson wrote:
> Bit Twister <BitTw...@mouse-potato.com> wrote:
>> I am trying to use a variable in an alias for xterm.
>> 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\"'
>> Any suggestions welcome.
>
> You don't need to quote double-quotes within single-quotes.
>
> $ alias xterm=' ... ; /usr/bin/xterm -title "$PWD dt=$dt"'

And there lies the problem. I removed the backslashes and it works as
designed. :)

Alan Curry's reply explains the original failure.

A "Thank you" to all who replied.

Alan Curry

unread,
Nov 22, 2012, 8:48:18 PM11/22/12
to
In article <u2f2o9-...@cfa.johnson>,
That doesn't work when there's an apostrophe in the arguments. (And I'm not
sure why you want to print $0 in an echo-like script. echo doesn't print its
own name before everything.)

Mine is broken too when there are backslashes in the arguments. Fix:

#!/usr/bin/zsh -f
setopt bsdecho # BSD echo is the only sane echo!
echo ${(qq)@}

Different quoting styles can be requested with (q), (qq), (qqq), (qqqq)

--
Alan Curry

Chris F.A. Johnson

unread,
Nov 22, 2012, 9:11:51 PM11/22/12
to
On 2012-11-23, Alan Curry wrote:
> In article <u2f2o9-...@cfa.johnson>,
> Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>>On 2012-11-22, Alan Curry wrote:
>>>
>>> You could make a helper script to echo an entire command with quotes:
>>>
>>> $ cat echoquoted
>>> #!/usr/bin/zsh -f
>>> echo ${(qq)@}
>>
>> Or in any shell:
>>
>>$ cat eq
>>
>>printf "'%s' " "${0##*/}" "$@"
>>echo
>
> That doesn't work when there's an apostrophe in the arguments.

It does for me. (What would you expect to happen when there is an
apostrophe in the arguments?)

> (And I'm not sure why you want to print $0 in an echo-like script.
> echo doesn't print its own name before everything.)

I don't; I misread a line from your previous post, and emulated
that.

> Mine is broken too when there are backslashes in the arguments.

Mine works no matter what is in the arguments.

I sometimes use a function (written for my book, "Pro Bash
Programming"):

sa()
{
pre=:
post=:
printf "$pre%s$post\n" "$@"

Alan Curry

unread,
Nov 23, 2012, 12:28:22 PM11/23/12
to
In article <7k23o9-...@cfa.johnson>,
Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>On 2012-11-23, Alan Curry wrote:
>> In article <u2f2o9-...@cfa.johnson>,
>> Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>>>On 2012-11-22, Alan Curry wrote:
>>>>
>>>> You could make a helper script to echo an entire command with quotes:
>>>>
>>>> $ cat echoquoted
>>>> #!/usr/bin/zsh -f
>>>> echo ${(qq)@}
>>>
>>> Or in any shell:
>>>
>>>$ cat eq
>>>
>>>printf "'%s' " "${0##*/}" "$@"
>>>echo
>>
>> That doesn't work when there's an apostrophe in the arguments.
>
> It does for me. (What would you expect to happen when there is an
> apostrophe in the arguments?)

Here they are together:

$ cat echoquoted
#!/usr/bin/zsh -f
setopt bsdecho
echo ${(qq)@}
$ cat eq
#!/bin/sh
printf "'%s' " "$@"
echo
$ echo hello > foo\'bar
$ cat foo\'bar
hello
$ ./echoquoted cat foo\'bar
'cat' 'foo'\''bar'
$ ./eq cat foo\'bar
'cat' 'foo'bar'
$ 'cat' 'foo'\''bar'
hello
$ 'cat' 'foo'bar'
>

The zsh (qq) modifier correctly used the 4-character sequence '\'' which
resulted in an echoed command which works like the original. Yours didn't.
The last prompt is a ">" because there are an odd number of apostrophes. The
command isn't finished.

--
Alan Curry
0 new messages