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:

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Chema Jun 20, 2012 @ 14:51

    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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.