28

I have a bash variable: agent1.ip with 192.168.100.137 as its value. When I refer to it in echo like this:

echo $agent1.ip

the result is:

.ip

How can I access the value?

UPDATE: my variables are:

enter image description here

8
  • 10
    How did you get the . in the variable name in the first place? Commented Oct 22, 2014 at 11:37
  • 3
    Indeed, as @TomFenech hints at that's not a valid variable name. It shouldn't be possible to have a variable with that name at all. Commented Oct 22, 2014 at 11:40
  • 1
    i'm using a tool that run my bash and it has env variable with dotted name... Commented Oct 22, 2014 at 11:42
  • 2
    Can you confirm you have that variable? It might be that your tool is not creating the variable you think it is. Commented Oct 22, 2014 at 11:45
  • 3
    bash.ip=192.168.100.137 is a perfectly valid string to include in the environment. It's just not a string which bash can automatically turn into a variable. Commented Oct 22, 2014 at 13:11

6 Answers 6

23

Bash itself doesn't understand variable names with dots in them, but that doesn't mean you can't have such a variable in your environment. Here's an example of how to set it and get it all in one:

env 'agent1.ip=192.168.100.137' bash -c 'env | grep ^agent1\\.ip= | cut -d= -f2-'
Sign up to request clarification or add additional context in comments.

3 Comments

Then you can assign that value to a valid bash variable name.
The grep regex is wrong, please quote the \ or it will also accept agent1_ip or agent1-ip, etc
@drizzt: you're right, I added another `` to properly escape the pattern.
16

Since bash.ip is not a valid identifier in bash, the environment string bash.ip=192.168.100.37 is not used to create a shell variable on shell startup.

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

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

Comments

10

The cleanest solution is:

echo path.data | awk '{print ENVIRON[$1]}'

2 Comments

This duplicates previous answers like this one from 2014.
Both answers use the awk's ENVIRON variable, more here , here and here and here. I guess it's not a duplicate of the other answer in that: a) this answer is "shorter" and b) does something different (echos, vs assigns)
6

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

Dotted variable names are an advanced feature in ksh93. A simple case is

$ a=1
$ a.b=123
$ echo ${a.b}
123
$ echo $a
1

If you first attempt to assign to a.b, you'll get

 -ksh: a.b=123: no parent

IHTH

Comments

5

Try this:

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

1 Comment

Note that . in grep means "any character," so this will behave badly if there is e.g. agent1Sport. Likewise flagent1.port or blagent1Sportier. See my answer for a more exact expression.
1

For accessing the environment variable in a readable way, I'd make a shell function of it:

getenv() { 
    awk 'BEGIN {print ENVIRON[ARGV[1]]}' "$1";
}

You can then use it as

$ getenv agent1.ip
192.168.100.137
$ myvar=$(getenv agent1.ip)
$ echo "$myvar"
192.168.100.137

Differences with the accepted answer are:

  • robustness: supports also newlines in the variable value (grep and cut work on lines)
  • efficiency: only one fork (awk) instead of 3 (env, grep, cut) in a pipeline (which also has to set up i/o buffers)

There is also another question about Exporting a variable with dot (.) in it.
Short answer: you can't, but you needn't, except for calling another executable. In that case use env.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.