There is a much more concise way to do this. Arguments to a bash script can be brought into an array, which makes dealing with the elements much simpler. The script below will always print the last argument passed to a script.
argArray=( "$@" ) # Add all script arguments to argArray
arrayLength=${#argArray[@]} # Get the length of the array
lastArg=$((arrayLength - 1)) # Arrays are zero based, so last arg is -1
echo ${argArray[$lastArg]}
There is a much more concise way to do this. Arguments to a bash script can be brought into an array, which makes dealing with the elements much simpler. The script below will always print the last argument passed to a script.
Sample output
Use indexing combined with length of:
A solution using
eval
:Here is mine solution:
evileval
Code:
This format can worked in Slackware and Cygwin.
"${x[@]:(-1)}", if used with $@, "${@:(-1)}"
It means is: ${@:(N)}, will return all element after N index.(include N), -1 is thelast.
The simplest answer for bash 3.0 or greater is
That's it.
Output is: