linux - 在 Bash 中,为什么 `x=100 echo $x` 不打印任何内容?

标签 linux bash shell variables scope

<分区>

我看到这样的代码:

fqdn='computer1.daveeddy.com'

IFS=. read hostname domain tld <<< "$fqdn"
echo "$hostname is in $domain.$tld"
# => "computer1 is in daveeddy.com"

我认为它有效,因为 IFS 在第三行分配给了 .。所以我尝试了这个:

x=100  echo $x

但是发现 bash 没有打印任何东西,而我预计它会打印 100..

此外,我发现x=100 echo $x; echo $x 什么都不打印,而 x=100; echo $x 打印出 100,这非常令人困惑。

有人对此有想法吗?

最佳答案

$x echo 运行之前展开,并将结果作为参数传递给 echo . echo 在其环境中不使用 x 的值。

在第一个示例中,read 在其环境中使用 IFS 的值来拆分它通过 here 字符串接收的字符串。

关于linux - 在 Bash 中,为什么 `x=100 echo $x` 不打印任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29483011/

相关文章:

linux - 用于迁移配置文件变量的 Shell 脚本

c - GNU Make 无法从循环中进入子目录

Bash 重定向运算符 sed 命令

bash - mvn 命令返回 : [ERROR] The goal you specified requires a project to execute but there is no POM in this directory

linux - 检查文件是否真的可以在 bash 中打开

linux - 函数在 shell 脚本中抛出错误 "Syntax error: "}"unexpected"

linux - 从 `ls` 中删除前 3 列?

php - 编译错误: "g++: error trying to exec ' cc1plus': execvp: No such file or directory"

python - 无法使用 paramiko 模块 ssh 到远程服务器

bash - 在参数中使用通配符选项为 bash 脚本创建别名