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

What does -- exec wish "$0" ${1+"$@"} -- mean?

827 views
Skip to first unread message

ghjs...@gmail.com

unread,
Oct 15, 2012, 2:53:27 AM10/15/12
to
$0 and $@ represent what? and 1+"$@" ?
Is it similar to the "argv" and "argc" in the c main function?
Thanks!

Arjen Markus

unread,
Oct 15, 2012, 3:52:39 AM10/15/12
to
$0 is the name of the shell script (so in this case your Tcl program) and
the magic sequence ${1+"$@"} is translated into the arguments that follow (if any). I admit that I do not understand the syntax, but it works.
The essence is:
$0 --> Tcl's argv0
${1+"$@"} --> Tcl's argv

(So similar to argv[0] and argv+1 in the C main function)

Regards,

Arjen

s.effe...@googlemail.com

unread,
Oct 15, 2012, 8:54:39 AM10/15/12
to
This is due to a bug in some shell versions and lengthily explained in "exec magic" here:

http://wiki.tcl.tk/812

Simon Guo

unread,
Oct 16, 2012, 4:20:29 AM10/16/12
to
On Monday, October 15, 2012 8:54:39 PM UTC+8, (unknown) wrote:
> This is due to a bug in some shell versions and lengthily explained in "exec magic" here:
>
>
>
> http://wiki.tcl.tk/812
Great! The answer:

The incantation looks like this:
exec wish "$0" ${1+"$@"}
Here's how it works:
Each argument to sh is numbered $1, $2, $3, ... $9.
$0 is the name of the script. We put it in quotes to preserve whitespace and other weird characters.
The special variable $* expands to all the arguments. However, it expands *unquoted*, meaning that spaces will be lost in the expansion.
This means that the naive:
exec wish "$0" $*
will not work properly if any argument has a space in it. Likewise, using "$*" will fail because that will expand to a single argument.
The special form "$@" was introduced (the quotes are part of it) to work around this problem. This form expands to all the arguments, but properly (and individually) quoted. So why doesn't the following attempt work?
exec wish "$0" "$@"
The reason is that some versions of sh have a bug. If there are no arguments, these broken sh's will still expand "$@" into a single empty argument. Oops.
The sh syntax ${VAR+VALUE} expands to VALUE if $VAR is set, and nothing otherwise. We abuse this syntax to work around the buggy sh implementations. Recall that the first argument is named $1. If it exists, then we use "$@"; otherwise we do nothing:
exec wish "$0" ${1+"$@"}
The above is my interpretation. No doubt further refinements and clarifications are in order.

Simon Guo

unread,
Oct 16, 2012, 4:22:54 AM10/16/12
to
You're right, and the {1+"$@"} form is used to avoid some sh bugs.

Donal K. Fellows

unread,
Oct 16, 2012, 8:13:46 PM10/16/12
to
On 16/10/2012 09:20, Simon Guo wrote:
> So why doesn't the following attempt work?
> exec wish "$0" "$@"
> The reason is that some versions of sh have a bug.

It should probably be noted that it's not at all clear if anyone has
those versions of sh any more (except as historical curiosities). There
is a lot of cargo-cult programming done with shells, as their syntax is
really rather difficult to get correct when robustness is desired.

Donal.

Arjen Markus

unread,
Oct 17, 2012, 3:32:35 AM10/17/12
to
Indeed, just for fun, try to read and understand the typical code generated
by autotools in the ./configure file. It is not impossible, but it is not
the easiest prose in the world either.

Regards,

Arjen

Uwe Klein

unread,
Oct 17, 2012, 4:31:57 AM10/17/12
to
Historic fixes won't be removed until they themselves start to clash.
( And quite often another fix will be plastered of the original one instead )


uwe
0 new messages