#!/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
Run Code Online (Sandbox Code Playgroud)
我们运行这个:
./trash.sh
./trash.sh: line 3: =/home/someuser/trash: No such file or directory
Run Code Online (Sandbox Code Playgroud)
如何在没有bash评估的情况下将此文件名存储在变量中?另外:我如何在以后的测试中使用此变量?
if [ -e $TRASHDIR ]
then
echo stuff here
fi
Run Code Online (Sandbox Code Playgroud)
定义 var $时,请先关闭初始字符:
TRASHDIR=$HOME/trash
Run Code Online (Sandbox Code Playgroud)
当你使用时$TRASHDIR,将其bash评估为空字符串(或者先前设置的其他内容),这就是为什么它看起来像:
=/home/someuser/trash
Run Code Online (Sandbox Code Playgroud)