Re: What's the last argument?

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/30/03


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"]


Relevant Pages

  • Re: Run-Time Error 3061...Too few parameters. Expected 2
    ... I'm getting a run time error. ... I've used single quotes around the value from ... double-quote character instead, or you can use the Replacefunction to ...
    (microsoft.public.access.formscoding)
  • Re: ComboBox Selection
    ... You will run into problems if your string contains the same character you ... are using as a delimiter. ... That's 2 single quotes, not one double quote. ...
    (microsoft.public.access.forms)
  • Re: Beginners Program
    ... I'd put single quotes aroung the, ... If you are putting "s in a string then it is usually best to use a different ... input file things could go wrong. ... and that the final line of input does end with a newline character. ...
    (comp.lang.perl.misc)
  • Re: can I know how to write a html parser in C
    ... On each iteration of the loop, fetch one character ... then transit to state 3 else transit to state 4 ... you are not presently in quotes. ... You must account for comments! ...
    (comp.lang.c)
  • Re: how do i sort data record on two rows?
    ... quotes " were not the characters expected. ... If necessary, copy the following character: ... ino ne sauti ya anyole okhurula ebusikhale emmayoka. ...
    (microsoft.public.excel.worksheet.functions)