A reference to a bash variable whose name contains a dot

I have a bash: agent1.ip with 192.168.100.137 as its value. When I reference it in echo :

 echo $agent1.ip 

result:

 .ip 

How to access the value?

UPDATE: my variables:

enter image description here

+6
source share
4 answers

Bash itself does not understand variable names with dots in them, but this does not mean that you cannot have such a variable in your environment. Here is an example of how to install it and get it all in one:

 env 'agent1.ip=192.168.100.137' bash -c 'env | grep ^agent1\\.ip= | cut -d= -f2-' 
+9
source

Try the following:

 export myval=`env | grep agent1.port | awk -F'=' '{print $2}'`;echo $myval 
+2
source

Since bash.ip not a valid identifier in bash , the bash.ip=192.168.100.37 environment line is not used to create a shell variable when the shell starts.

I would use awk , a standard tool to extract value from the environment.

 bash_ip=$(awk 'BEGIN {print ENVIRON["bash.ip"]}') 
+2
source

Is your code nested and uses functions or scripts that use ksh?

Dotted variable names are an extended function in ksh93. Simple case

 $ a=1 $ ab=123 $ echo ${ab} 123 $ echo $a 1 

If you try to assign ab , you will get

  -ksh: ab=123: no parent 

Ihth

0
source

Source: https://habr.com/ru/post/977073/


All Articles