阻止BASH评估变量中的文件名

时间:2012-08-07 07:52:38

标签: bash variables

#!/bin/bash
#trash.sh

$TRASHDIR=$HOME/trash 
#Does not exist yet, i have loops further in 
#the script to check if the dir exists and to create it if not
echo $TRASHDIR
#I have ommited the rest of the script, 
#because this is where I have the problem

让我们运行:

./trash.sh  
./trash.sh: line 3: =/home/someuser/trash: No such file or directory

如何在没有bash评估的情况下将此文件名存储在变量中?另外:我如何在以后的测试中使用此变量?

if [ -e $TRASHDIR ]
then
  echo stuff here
fi

1 个答案:

答案 0 :(得分:5)

定义 var时,请关闭初始的$字符:

TRASHDIR=$HOME/trash

当你使用$TRASHDIR时,bash将其评估为空字符串(或者先前设置的其他内容),这就是它的原因:

=/home/someuser/trash