获取传递给 shell 脚本的最后一个参数 [英] Getting the last argument passed to a shell script

查看:40
本文介绍了获取传递给 shell 脚本的最后一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$1 是第一个参数.
$@ 就是所有这些.

$1 is the first argument.
$@ is all of them.

如何找到传递给 shell 的最后一个参数脚本?

How can I find the last argument passed to a shell script?

推荐答案

这有点坑:

for last; do true; done
echo $last

这个也很便携(同样,应该与 bash、ksh 和 sh 一起使用)并且它不会改变参数,这可能很好.

This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.

它使用 for 隐式循环参数的事实,如果你不告诉它循环什么,以及 for 循环变量没有作用域的事实:它们保留最后一个值他们被设置为.

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.

这篇关于获取传递给 shell 脚本的最后一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆