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 $@?
- See our previous faq for more information: Bash Get All Command Line Arguments Before Last Parameter In $@
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop







![Linux / Unix: Show Shares on NFS Server [ Shared Directories ]](http://s13.cyberciti.org/images/shared/rp/3/20.jpg)

![Unix Copy Command Examples [ cp command ]](http://s13.cyberciti.org/images/shared/rp/3/22.jpg)



{ 1 comment… read it below or add one }
try to use $#
example:
last_id=$# last_element=${@:last_id} echo all $@ echo last_id $last_id echo last_element $last_elementRun