How can I read a variables file into a bash script?

5,460

I saw this technique:

read VAR1 VAR2 < path/to/input/file.txt
echo $VAR1
echo $VAR2

Source:

https://stackoverflow.com/questions/29270289/bash-read-from-file-and-store-to-variables

Share:
5,460

Related videos on Youtube

Florian van Oudgaarden
Author by

Florian van Oudgaarden

Updated on September 18, 2022

Comments

  • Florian van Oudgaarden
    Florian van Oudgaarden almost 2 years

    I'm creating bash scripts to copy files search errors etc. from all our test servers.

    I want to create an input file for these servers because they’re all test systems so they change hostname and IP from time to time.

    Currently, I have to change all scripts if a server is changed.

    I want one input file so I only have to change it there. So I only have to change the input file.

    My input file I have created looks like this:

    #!/bin/bash
    SERVER1=$(echo "node1.example.com")
    SERVER2=$(echo "node2.example.com")
    etc.
    

    I have tried

    export file="path/to/input/file"
    

    and also

    read file="path/to/input/file"
    

    but it doesn't work.

    Now I want to read the values $SERVER1, $SERVER2, etc. into my other scripts.

    How can I do that?

    • Melebius
      Melebius almost 7 years
      Note that the command substitution combined with echo is redundant. You can write just SERVER1=node1.example.com.