bash 从主机名分配变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34537573/
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 14:04:19  来源:igfitidea点击:

Assigning variable from hostname

bashvariablescentos

提问by perseusraz

I'm trying to assign the hostname of a CentOS 6 box as a variable. I have:

我正在尝试将 CentOS 6 机器的主机名分配为变量。我有:

BOX="hostname"
echo $BOX
rm -rfv /etc/hosts
cp /hardware/dats/files/$BOX/hosts /etc

I have also tried

我也试过

BOX='hostname'

and

BOX= hostname

But the variable doesn't seem to stick. No error, it just doesn't copy. Any thoughts?

但变量似乎没有坚持。没有错误,它只是不复制。有什么想法吗?

回答by Barmar

If you want to put the name of the host in the BOXvariable, rather than the literal string hostname, use command substitution:

如果你想把主机名放在BOX变量中,而不是字符串中hostname,使用命令替换

BOX=$(hostname)