【问题标题】:What does ${1-1} in bash mean?bash 中的 ${1-1} 是什么意思?
【发布时间】:2013-12-30 09:20:05
【问题描述】:

我正在阅读来自here 的脚本并试图了解发生了什么。此函数执行更改 Finder 窗口的目录:

function ee { 
 osascript -e 'set cwd to do shell script "pwd"'\
 -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "set the target of window ${1-1} to (POSIX file cwd) as string"\
 -e 'else' -e "open (POSIX file cwd) as string"\
 -e 'end if' -e 'end tell';\
};\

我假设 $ 由 bash 解释,因为它在双引号内。我无法找到{1-1} 的含义。我已经在单独的测试脚本中使用过该表达式,但找不到与普通 $1 的区别。有什么想法吗?

【问题讨论】:

  • 相信这是一个“默认”。如果用户提供了一个命令行参数,那么${1-1} 将扩展为该参数,否则它将简单地扩展/“默认”为连字符之后的任何内容——在你的情况下为1

标签: bash osascript


【解决方案1】:

这意味着如果参数 1 (${1}) 未设置,则将值设置为 1。

parameter substituiton here

 ${parameter-default}, ${parameter:-default}
   If parameter not set, use default.

【讨论】:

  • +1,请注意:有人建议我 here 说,“ABS 不是一个很好的参考——它在示例中显示了很多不好的做法。最好将人们指向 mywiki。 Wooledge.org/BashFAQ/073 "
  • @anishsane 谢谢你的参考。 tldp.org 中的示例非常糟糕。但他们的布局更简洁恕我直言。
猜你喜欢
  • 2015-09-27
  • 2019-10-29
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多