-   Programming (
-   -   Finding the last command line argument (bash) (

pete1234 09-30-2006 07:19 PM

Finding the last command line argument (bash)
Is there a simple way to do it? Any help is appreciated.

comprookie2000 09-30-2006 07:25 PM

I press the up arrow key. Is that what you mean?

pete1234 09-30-2006 07:31 PM

I'm sorry, I meant inside a script.

druuna 09-30-2006 07:37 PM


$# gives you the amount of arguments. I.e:
$ ./mysript a b c

$* gives you all the args in a string. I.e:
$ ./myscript a b c
a b c

See bash manpage for details.

Both or the last could be of use.

You can also take a look at shift, also a bash internal.

Don't know if you are writing/wrote this script, but if arguments become important and you need it to be more flexible also take a look at getopts (bash internal, see manpage/bash book).

Hope this helps.

Dark_Helmet 09-30-2006 07:42 PM



echo ${BASH_ARGV[0]}


$ ./test_script 1 2 3 test
$ ./test_script 1 2 3 test foobar

makyo 09-30-2006 07:44 PM

Hi, pete1234.

With some features found in the shell, you can.



# @(#) s1      Demonstrate how to obtain last positional parameter.

set -- 1 2 3 4four 5 6 7 8 9 10 11 12 13 14 15x

echo "All parameters:"
echo ":$*:"

echo " Show that simple dereferencing does not work above 9."
echo $4
echo $15

echo " Number of last parameter is :$LAST:"

echo " Save all parameters."

echo " Shifting all parameters."
shift $(( $LAST-1 ))

echo ' Now $1 is the old $15'
echo $1

echo 'All parameters still in SAVE, and can be re-established with "set -- $SAVE"'
echo ":$SAVE:"

When run, this produces:

% ./s1
All parameters:
:1 2 3 4four 5 6 7 8 9 10 11 12 13 14 15x:

 Show that simple dereferencing does not work above 9.

 Number of last parameter is :15:
 Save all parameters.

 Shifting all parameters.

 Now $1 is the old $15

All parameters still in SAVE, and can be re-established with "set -- $SAVE"
:1 2 3 4four 5 6 7 8 9 10 11 12 13 14 15x:

The 4 and 15 have extra characters to make sure we're doing the right thing. Note that "$*" and "$@" are special to the shell. Use whichever one in appropriate. See man bash for details.

Best wishes ... cheers, makyo

makyo 09-30-2006 07:46 PM


I like Dark_Helmet's solution, although I could not get that to work in bash version 2.05b.0(1) ... cheers, makyo

pete1234 09-30-2006 07:48 PM

I've tried using shift it doesn't seem to help. All arguments manipulate the last command line argument which is a file, so as I shift it screws things up(maybe I just don't know how to use shift properly?). I've also tried several variations of

to no avail. The closest I've come is using something like

echo $* | egrep -o 'regex'
. That normally works, but it seems like there's a better way.

#EDIT I guess there was no need for this rant. Thanks for the help.

pete1234 09-30-2006 07:52 PM

Dark_Helmet could you explain your solution?

druuna 09-30-2006 07:53 PM


You should use Dark_Helmet's method, it's the best one. I though using ARGV was only possible in perl, my bad.

Still, here's how you use $#, $* and shift in a nutshell:

echo $numArgs
echo $*
shift 2
echo $*

Sample run:

$ ./script a b c
a b c

Dark_Helmet 09-30-2006 08:02 PM

BASH_ARGV is a special built-in variable. It's an array of the command line arguments. More specifically, the man page says it's the arguments located on the stack. So it's safe to read them, but I strongly suggest not modifying them.

To access the array, use [X] to refer to a specific argument. The arguments are put on the stack "backwards" and explains why the last argument is indexed with 0.

Lastly, BASH_ARGV will change inside a function call (because the stack changes). So be careful where/how you access the variable.

druuna 09-30-2006 08:09 PM


Which bash/bash version are you using?

BASH_ARGV isn't part of GNU bash, version 2.05b.0(1)-release (i686-pc-linux-gnu), like makyo I can't get it to work with this release.

pete1234 09-30-2006 08:11 PM

Works for me

bash --version
GNU bash, version 3.1.0(1)-release (i686-pc-linux-gnu)

Thanks everyone for the help.

druuna 09-30-2006 08:26 PM

Time for an upgrade :)

Dark_Helmet 09-30-2006 08:41 PM

Sorry for the late reply... Looks like it might have been a feature for version 3.

$ bash --version
GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.

This is on my FC4 system.

All times are GMT -5. The time now is 08:38 AM.