≡ Menu

Bash: Get The Last Argument Passed to a Shell Script

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 $@?

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 1 comment… add one }
  • Chema June 20, 2012, 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
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , , , ,