繁体   English   中英

Bash 指令变量赋值

[英]Bash command variable assignment

如果我分配一个变量:

testThis='echo "This is a test"'

如果我使用$testThis ,它可以在脚本中运行。

但是,如果我想跳过一行怎么办? 所以我尝试:

testThis='echo; echo "This is a test"'

这失败了!

经过大量尝试$()命令替换和各种引用后无法弄清楚。

不重新解析参数扩展。 以下

testThis='echo; echo "This is a test"'
$testThis

相当于

echo ';' echo \"This is a test\"

分号和双引号都是字符串的文字部分,而不是 shell 语法。 分词和去除引号后,shell 用六个 arguments 识别命令echo显:

  1. ;
  2. echo
  3. "This
  4. is
  5. a
  6. test"

使用 function 代替:

testThis () {
    echo; echo "This is a test"
}

testThis

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM