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
Assigning variable from hostname
提问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 BOX
variable, rather than the literal string hostname
, use command substitution:
如果你想把主机名放在BOX
变量中,而不是字符串中hostname
,使用命令替换:
BOX=$(hostname)