0

I source these variables from a txt file in CentOS bash shell:

NAME01=dns1
HOST01=1.1.1.1
NAME02=dns2
HOST02=1.1.2.2
NAME03=dns3
HOST03=1.1.2.3
## many more lines of similar

NUM=02

How can I use $HOST$NUM to give me 1.1.2.2, such as:

echo $HOST$NUM
0

1 Answer 1

2

It seems you are looking for associative arrays, look:

declare -A host
host[1]=1.1.1.1
host[2]=1.1.2.2
host[3]=1.1.2.3
num=2
echo "${host[$num]}"

Or even simple array:

host=( 
    1.1.1.5
    1.1.1.1
    1.1.2.2
    1.1.2.3
)
num=2
echo "${host[num]}"

Look: https://web.archive.org/web/20181220163244/http://wiki.bash-hackers.org/syntax/arrays


'eval' is a common misspelling of 'evil'. If eval is the answer, surely you are asking the wrong question. See http://mywiki.wooledge.org/BashFAQ/048

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.