导出变量,这些变量是shell中的变量值

时间:2013-05-06 08:56:02

标签: linux bash shell variables export

当我尝试这样做时......

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

有效。

但我想这样做..

$ 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

这是我得到的错误。 有没有办法处理变量的值导出.. [[注意:如果路径没有空格,它的工作完美! ]

1 个答案:

答案 0 :(得分:3)

请勿使用$。否则,shell会在将变量传递给export之前扩展变量的值。以下内容适用:

export my_variable

在评论中,它指出你不想将变量赋值本身存储为变量,然后将其传递给export。这是一个例子:

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

# check if it succeeded 
export | grep foo # output: declare -x foo="bar"