Thursday, December 18, 2014

Shell Scripting : How to find the value of the last argument passed to a shell script?

Curly Bracket notation {} for positional parameters helps in identifying the value of the last argument passed to a shell script

#!/bin/bash
#argcheck.sh

args=$#          #Specifies the number of arguments passed to the script

lastarg=${!args} #Specifies the value of the last argument passed to the script

# lastarg=${!#} is another way to get the last argument value

echo $args

echo $lastarg

If the above script is run as

./argcheck.sh 10 20 30

3  - Specifies the number of arguments passed to the function
30 - Specifies the value of the last argument

No comments:

Post a Comment