-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 !

Engine
  • 4,930
  • 14
  • 65
  • 140
  • 4
    Possible duplicate of [How to assign the output of a Bash command to a variable?](https://stackoverflow.com/q/2314750/608639), [Assigning the output of a command to a variable](https://stackoverflow.com/q/20688552/608639), etc. – jww Nov 20 '19 at 09:38

5 Answers5

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"
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
1

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.

jBuchholz
  • 1,444
  • 1
  • 12
  • 19
0

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

Dominique
  • 8,687
  • 9
  • 28
  • 67
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}.

-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"
ChuckCottrill
  • 3,999
  • 2
  • 22
  • 34