LinuxQuestions.org

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

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

Hi,

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

$* 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

Code:

#!/bin/bash

echo ${BASH_ARGV[0]}

Code:

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


makyo 09-30-2006 07:44 PM

Hi, pete1234.

With some features found in the shell, you can.

Consider:
Code:

#!/bin/sh

# @(#) 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
echo " Show that simple dereferencing does not work above 9."
echo $4
echo $15

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

echo " Save all parameters."
SAVE="$@"

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

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

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

When run, this produces:
Code:

% ./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.
4four
15

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

 Shifting all parameters.

 Now $1 is the old $15
15x

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

Hi.

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
Code:

$$#
to no avail. The closest I've come is using something like
Code:

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

Hi,

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:
Code:

#!/bin/bash
numArgs="$#"
echo $numArgs
echo $*
shift 2
echo $*

Sample run:
Code:

$ ./script a b c
3
a b c
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

@Dark_Helmet:

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
Code:

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.
Code:

$ 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 03:19 PM.