3

When i try to do this...

$ export my_path='/my/path\ with\ spaces/file'
$ echo $my_path`
/my/path\ with\ spaces/file

It works.

But i want to do this..

$ echo $my_variable
my_path='/my/path\ with\ spaces/file'
$ export $my_variable
-bash: export: `with\': not a valid identifier
-bash: export: `spaces/file'': not a valid identifier

This is the error iam getting. Is there a way to treat the value of the variable for exporting.. [[ NOTE: if the path did not have a space, it works perfectly!! ]]

1 Answer 1

3

Don't use the $. Otherwise the shell would expand the variable's value before passing it to export. The following will work:

export my_variable

In comments, it points out that you wan't to store the variable assignment itself as a variable and then pass it to export. Here comes an example:

var='foo=bar'
export "$var"

# check if it succeeded 
export | grep foo # output: declare -x foo="bar"
Sign up to request clarification or add additional context in comments.

5 Comments

This works because export will expand the value of its arguments. When you say export $foo then foo will be expanded before being passed to export and if foo contains spaces, export will think it's given several arguments that are names of shell variables.
actually, my end goal is to export the "my_path" variable. Not the "my_variable" itself.
Something like this? : $var='$foo=bar'; export "$var"
Not working man.. :( bash: export: '$foo=bar': not a valid identifier
var='foo=bar'; export "$var"

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.