Re: What's the last argument?
From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/30/03
- Next message: Heiner Steven: "Portability of "getent" [was: finding numeric GID of a group]"
- Previous message: Stephane CHAZELAS: "Re: finding numeric GID of a group"
- In reply to: V. Mark Lehky: "Re: What's the last argument?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 Nov 2003 13:40:36 +0100
2003-11-30, 09:36(+01), V. Mark Lehky:
> "Chris F.A. Johnson" wrote:
> [snip]
>> eval cd \"\${$#}\"
>>
>> Or:
>>
>> eval dest=\"\${$#}\"
>> eval cd "$dest"
>
> Thanx! I suspected 'eval' was the key, although I would have never gotten
> the quotes + escapes right.
That's more legible like this:
eval "dest=\${$#}"
The point is to have eval get passed a string like:
dest=${123}
where 123 is the expansion of $#. Chris' solution gives
dest="${123}"
to eval, which is OK too even if the quotes are not necessary.
But he didn't quote $# (which is OK (unless you modify IFS) in
practice but semantically not correct, as a non-quoted variable
is considered as a list of files), and didn't quote the '{' and
'}' characters which is OK in practice, but not much wise as
those characters are special in many other places in many
shells).
if in doubt, you can put backslashes before every special
character (or even every character) and put every variable
reference inside double quotes:
eval \d\e\s\t\=\"\$\{"$#"\}\"
Using double quotes, you remove every special meaning to every
character except ", \, $ and `, so you can do:
eval "dest=\"\${$#}\""
With single quotes you remove every special meaning to every
character, so:
eval 'dest="${'"$#"'}"'
Note that we first get out of single quotes to be able to pass
"$#", then get back to single quoted string.
Another portable solution to get the last arg is:
eval 'set "" ${1+"$@"}; shift '"$#"
Then you get the last arg in "$1"
-- Stéphane ["Stephane.Chazelas" at "free.fr"]
- Next message: Heiner Steven: "Portability of "getent" [was: finding numeric GID of a group]"
- Previous message: Stephane CHAZELAS: "Re: finding numeric GID of a group"
- In reply to: V. Mark Lehky: "Re: What's the last argument?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|