I have the following code:

export VAR1=FINAL_VAL="\"--value1 --value2\""
export $VAR1

After executing this I would expect FINAL_VAR=--value1 --value2, but on the second export I get "bash: export: `--value2"': not a valid identifier", like if quotation marks where not detect correctly, and if I execute

echo export $VAR1

I get

export FINAL_VAL="--value1 --value2"

Which if copy and paste it into the console I will get FINAL_VAR=--value1 --value2, however when executing from export (export $VAR1) or from (`echo export $VAR1`) It does not export.

It seems that it doesn't like/detect the quotation marks, but I tried scaping them many ways without any luck.

Did someone experienced something like this and could tell me how to workaround it?

Thanks beforehand,

有帮助吗?

解决方案

You have to do export "$VAR1", notice the quotes

其他提示

You can get rid of error

export: `--value2"': not a valid identifier 

by simple quotes:

export "$VAR1"

And reason its not getting exported when you do

(export $VAR1) 

or

(`echo export $VAR1`) 

because you are exporting it in a subshell () which exists as soon as command exits. You should not use it if you need to export some variable to shell environment, because subshell(a child of parent shell) cannot modify parent's environment.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top