LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Finding the last command line argument (bash) (https://www.linuxquestions.org/questions/programming-9/finding-the-last-command-line-argument-bash-488341/)

/bin/bash 10-05-2006 04:13 PM

Quote:

echo
echo " Show that simple dereferencing does not work above 9."
echo $4
echo $15
If you change that to:
echo ${15}
It will work.

Also $_ will give you the last argument of previous command.

$ cat doit
#!/bin/bash
echo ${20}

$ ./doit a b c d e f g h i j k l m n o p q r s t u v w x y z;echo "$_"
t
z

ygloo 10-05-2006 05:05 PM

#!/bin/bash

num="$#"
lastp=( echo $@ )

echo ${lastp[$num]}

makyo 10-05-2006 05:25 PM

Hi, /bin/bash.

Yes, ${15} worked on all the Bourne-related shells I found on (Debian) Linux: pdksh, dash, bash, bash-posix, zsh. It failed on sh/jsh on Solaris, but at least an error message was produced there.

I changed my demo script to include this illustration for future use.

Thanks ... cheers, makyo

makyo 10-05-2006 05:31 PM

Hi, ygloo.

Yes, that seemed to have worked even in my v2 bash. Inventive ... cheers, makyo

/bin/bash 10-30-2006 09:46 AM

I just noticed nobody mentioned ${!#}. Guess what ${!#} is? Yep, it's the last command line argument.

$cat doit
#!/bin/bash
echo "No. of command line arguments = $#"
echo "Last command line argument = ${!#}"

$./doit 1 2 3 4 last
No. of command line arguments = 5
Last command line argument = last

soggycornflake 10-30-2006 10:20 AM

Everyone seems to be using bash. I use zsh, in which parameters can be subscripted like python/etc.

i.e. the last argument is $argv[-1], or $@[-1], or $*[-1].


All times are GMT -5. The time now is 01:04 PM.