防止BASH从评估的变量名
问题描述:
#!/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
让我们来运行这个命令:防止BASH从评估的变量名
./trash.sh
./trash.sh: line 3: =/home/someuser/trash: No such file or directory
我如何可以存储这个文件名中没有庆典计算它的变量?另外:以后如何使用这个变量进行测试?
if [ -e $TRASHDIR ]
then
echo stuff here
fi
你也应该双引号你的变量插值。变量发生在美元符号的任何地方,你想把它放在双引号内。 – tripleee 2012-08-07 07:57:52
如果你使用'[[',即使它包含嵌入空格:'if [[-e $ TRASHDIR]]' – cdarke 2012-08-07 08:39:04