arrays - bash:为什么 ${@:-1} 的值与 ${@: -1} 不同?

标签 arrays bash

为什么在 :-1 之间添加空格会改变下面显示的行为?

(ins)$ set a b c d
(ins)$ echo ${@:-1}
a b c d
(ins)$ echo ${@: -1}
d
(ins)$

同样的行为也会影响 $*

我正在运行 GNU bash,版本 4.4.5(1)-release (x86_64-unknown-linux-gnu)

最佳答案

为避免参数扩展模式产生歧义:

${parameter:-word}

表示如果`参数未设置或为空,则替换word的扩展;否则替换参数的扩展。

所以对于切片操作,使用空格或括号:

$ set a b c d

$ echo "${@: -1}"
d

$ echo "${@:(-1)}"
d

关于arrays - bash:为什么 ${@:-1} 的值与 ${@: -1} 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660700/

相关文章:

bash - Cron 跳过 BASH 脚本的一部分

bash - 如何在whiptail中循环case语句

bash - shell 脚本 : with awk, 如果找到 string0 则写入 string1,否则写入 string2

将数组的信息复制到指针数组中

c# - 改进 O(n^2) 算法

arrays - 使用 Rails 4+ 和 PostgreSQL 数组检查非空数组

Bash 脚本、case 语句和子菜单

bash - 仅当 bash 中为空时才分配关联数组值

arrays - Swift - 动态结构数组

php - 我在通过引用使用递归函数更改数组时遇到问题