bash 带美元和不带美元的双括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31255699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:18:16  来源:igfitidea点击:

Double parenthesis with and without dollar

bash

提问by John Robins

Is $(...)the same as (...)in bash? Also is $((...))the same as ((...))? Also is ${...}the same as {...}? More generally what does the dollar sign stand for? Thank you.

$(...)一样的(...)在bash?也$((...))一样((...))吗?也${...}一样{...}吗?更一般地说,美元符号代表什么?谢谢你。

回答by John1024

  • $(...)means execute the command in the parens in a subshell and return its stdout. Example:

    $ echo "The current date is $(date)"
    The current date is Mon Jul  6 14:27:59 PDT 2015
    
  • (...)means run the commands listed in the parens in a subshell. Example:

    $ a=1; (a=2; echo "inside: a=$a"); echo "outside: a=$a"
    inside: a=2
    outside: a=1
    
  • $((...))means perform arithmetic and return the result of the calculation. Example:

    $ a=$((2+3)); echo "a=$a"
    a=5
    
  • ((...))means perform arithmetic, possibly changing the values of shell variables, but don't return its result. Example:

    $ ((a=2+3)); echo "a=$a"
    a=5
    
  • ${...}means return the value of the shell variable named in the braces. Example:

    $ echo ${SHELL}
    /bin/bash
    
  • {...}means execute the commands in the braces as a group. Example:

    $ false || { echo "We failed"; exit 1; }
    We failed
    
  • $(...)意味着在子shell中的parens中执行命令并返回其标准输出。例子:

    $ echo "The current date is $(date)"
    The current date is Mon Jul  6 14:27:59 PDT 2015
    
  • (...)意味着在子shell中运行括号中列出的命令。例子:

    $ a=1; (a=2; echo "inside: a=$a"); echo "outside: a=$a"
    inside: a=2
    outside: a=1
    
  • $((...))表示进行算术运算并返回计算结果。例子:

    $ a=$((2+3)); echo "a=$a"
    a=5
    
  • ((...))表示执行算术运算,可能会更改 shell 变量的值,但不返回其结果。例子:

    $ ((a=2+3)); echo "a=$a"
    a=5
    
  • ${...}表示返回括号中命名的 shell 变量的值。例子:

    $ echo ${SHELL}
    /bin/bash
    
  • {...}表示将大括号中的命令作为一个组执行。例子:

    $ false || { echo "We failed"; exit 1; }
    We failed
    

More generally what does the dollar sign stand for?

更一般地说,美元符号代表什么?

It means whatever it means in the given context.

它意味着在给定上下文中的任何含义。