-2

I'm trying to use the hostname of a linux device into a string for that here's what I'm trying:

app.sh

flowname="flow_${hostname}.txt"
echo "$flowname"

running the script delivers the following output : flow_.txt any idea what I'm missing here ? thanks in advance !

1

5 Answers 5

2

The ${VARIABLE} syntax is used to substitute the value of the given variable.

You want to execute the hostname command. By using the above variable substitution you try to output a variable called 'hostname' which does not exist.

You have to use the $(COMMAND) syntax. This will execute the given command and print its result.

Sign up to request clarification or add additional context in comments.

Comments

1

There's no built-in variable named $hostname. Try the variable $HOST or the command substitution $(hostname) instead.

flowname="flow_$HOST.txt"
flowname="flow_$(hostname).txt"

Comments

0

Try "uname -n", the "uname" command gives interesting information on your host.

Comments

0

I tried your script under ubuntu on WSL (windows subsytem for linux) and found the comment of John Kugelman useful.

To make the script functioning in this environment, i first print out the available system environment variables.

printenv | less 

then I chosen to replace ${hostname} with ${NAME}.

Comments

-1

You need variables that are populated. Use the command env to list environment variables,

env

So, just populate hostname,

hostname=$(uname -n)
echo ${hostname}
flowname="flow_${hostname}.txt"
echo "$flowname"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.