Shell Script: Get the last argument passed to the shell script








  • Method 1:
    last=$(eval "echo \$$#")
    
  • Method 2:
    last=${@:${#@}}
    
  • Method 3: Requires BASH 3.0+
    last=${!#}
    
  • Method 4: Requires BASH 3.0+
    last=$BASH_ARGV
    
  • Method 5: Portable solution. It uses the fact that for implicitly loops over the arguments if you don’t tell it what to loop over, and the fact that for loop variables aren’t scoped: they keep the last value they were set to.
    for last; do true; done
    



No comments:

Post a Comment