在bash脚本中导出环境变量 - 它不起作用

时间:2012-08-24 15:41:30

标签: bash shell command

我写了一个小的bash脚本来导出环境变量:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath = /home/katie/Desktop

if [ -n "$path" ]; then
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
else
    export my_var=$path
fi

但我收到了错误:

  

defaultPath:未找到命令

如何解决?

WORKNG VERSION:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath=/home/user/Desktop

if [ -n "$path" ]; then
    export my_var=$path
else
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
fi

1 个答案:

答案 0 :(得分:8)

在变量赋值中,=周围不允许有空格:

defaultPath=/home/katie/Desktop

对于空格,该行被解释为一个简单的命令,它尝试使用两个参数defaultPath=执行命令/home/katie/Desktop