【问题标题】:How can I skip the first argument in an ash/dash shell function?如何跳过 ash/dash shell 函数中的第一个参数?
【发布时间】:2019-08-17 00:54:39
【问题描述】:

在 ash/dash 函数中,我可以像这样引用完整的参数列表:

allparameters() { echo "$@"; }

这给了我:

$ allparameters yyyyy abffcd efgh
yyyyy abffcd efgh

我想跳过yyyyy,所以我尝试了${@:2}

butlast() { echo "${@:2}"; }

但是,这会跳过前两个字符:

$ butlast yyyyy abffcd efgh
yyy abffcd efgh
$ butlast abffcd efgh
ffcd efgh

我无法在 the man page 中找到 ash 的冒号语法,所以这可能是 bash 主义。什么是等价物?

【问题讨论】:

  • 看起来ash 正在将: 应用于由位置参数形成的单个字符串,而不是参数的“数组”。不确定这是错误还是预期(但未记录)的行为。在dash 中,这只是一个“错误替换”错误。
  • 有趣。谢谢你的上下文。

标签: shell ash dash-shell


【解决方案1】:

${name:offset}bashism,但您可以使用 POSIX shift 命令来满足您的需求。

$ butlast() { shift; echo "$@"; }
$ butlast foo bar baz
bar baz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 2011-04-04
    • 2015-04-21
    • 2017-04-23
    • 2013-10-02
    • 1970-01-01
    • 2017-12-12
    • 2020-12-07
    相关资源
    最近更新 更多