Bash: Get The Last Argument Passed to a Shell Script

by on June 19, 2012 · 1 comment· LAST UPDATED June 26, 2012

in

I'm writing a bash wrapper script that will pass arguments to the command. I need to find out the last argument if I call the wrapper as follows:

./wrapper -a -b --longarg=foo thisfilename.txt
./wrapper -a -b thisfilename.txt
./wrapper -a --next=true thisfilename.txt

Where,
=> $@ is all of them.
=> $0 is script name.
=> $1 is first arg.

I want thisfilename.txt stored in a shell variable called $last. How do I find the last argument passed to a shell script written in bash or ksh under Unix like operating systems?

You can find the last argument passed to a shell script using any one of the following syntax:

 
## only works with bash / ksh ##
echo "${@: -1}"
 

The following will only work with bash v3.x+:

 
echo "${BASH_ARGV[0]}"
 

A sample shell script:

 
#!/bin/bash
echo "Last argument only (works with bash/ksh only): ${@: -1}"
echo "Last argument only (works with bash 3.x+ only): ${!#}"
echo "Last argument only (works with bash 3.x+ only): $BASH_ARGV"
echo "Last argument only (works with bash 3.x+ / ksh only): ${@:$#}"
echo "Last argument only (works with bash 3.x+ only): ${BASH_ARGV[0]}"
echo -n "Last argument only (portable version): "
for i in $@; do :; done
echo "$i"
 

Run it as follows:
$ ./script -a -b --foo thisfilename.txt
Sample outputs:

Last argument only (works with bash/ksh only): thisfilename.txt
Last argument only (works with bash 3.x+ only): thisfilename.txt
Last argument only (works with bash 3.x+ only): thisfilename.txt
Last argument only (works with bash 3.x+ / ksh only): thisfilename.txt
Last argument only (works with bash 3.x+ only): thisfilename.txt
Last argument only (portable version): thisfilename.txt

Another test:
$ ./script -a -b --foo --next=true thisfilename.txt
Sample outputs:

Last argument only (works with bash/ksh only): thisfilename.txt
Last argument only (works with bash 3.x+ only): thisfilename.txt
Last argument only (works with bash 3.x+ only): thisfilename.txt
Last argument only (works with bash 3.x+ / ksh only): thisfilename.txt
Last argument only (works with bash 3.x+ only): thisfilename.txt
Last argument only (portable version): thisfilename.txt

A note about portable version

The following code:

 
for i in $@; do :; done
echo "Last arg : $i"
 

Can be written as follows:

 
#-----------------------------------------------------------------------------------------#
# How does it works? 
# If you do not tell what to loop over shell will loop over the arguments i.e. $@ .  
# This is a default. 
#-----------------------------------------------------------------------------------------#
for i; do :; done
echo "Last arg : $i"
 

It should work with sh, ksh, and bash without any problem.

How do I find out all command line args before last parameter in $@?

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 1 comment… read it below or add one }

1 Chema June 20, 2012 at 2:51 pm

try to use $#
example:

last_id=$#
last_element=${@:last_id}
echo all $@
echo last_id $last_id
echo last_element $last_element

Run

$ bash scriptexample a b c x y z
all a b c x y z
last_id 6
last_element z

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: