lova February 2016

Bash: How to put hostname between quotes in file

I try to add the hostname of my server in a file but it fails. I tried:

HOST=`hostname`
echo ' "server": "${HOST}"' >> /myfile

And I tried:

echo ' "server": "`hostname`"' >> /myfile

It's important to know that I need this output in my file:

"server": "ip-133..."

Answers


choroba February 2016

Single quotes don't interpolate variables. Use double or no quotes:

echo ' "server": "'"$HOST"'"'
echo ' "server": "'`hostname`'"'


mauro February 2016

Try this:

$ echo "\"server\": \"$(hostname)\"" >> myfile

Post Status

Asked in February 2016
Viewed 1,976 times
Voted 7
Answered 2 times

Search




Leave an answer