当我尝试这样做时......
$ 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
这是我得到的错误。 有没有办法处理变量的值导出.. [[注意:如果路径没有空格,它的工作完美! ]
答案 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"