Shell 确认主目录下是否存在目录

Shell 确认主目录下是否存在目录,shell,directory,quoting,Shell,Directory,Quoting,我想检查目录是否存在并编写下面的脚本,但这不能正常工作 #!/bin/sh if [ -d "~/sample" ] then echo 'exists' else echo 'NOT exists' fi 下面的脚本可以工作 #!/bin/sh if [ -d "/home/user01/sample" ] then echo 'exists' else echo 'NOT exists' fi 如果[-d“~/sample”]有什么问题吗?是的,双引号是不

我想检查目录是否存在并编写下面的脚本,但这不能正常工作

#!/bin/sh
if [ -d "~/sample" ]
then
    echo 'exists'
else
    echo 'NOT exists'
fi
下面的脚本可以工作

#!/bin/sh
if [ -d "/home/user01/sample" ]
then
    echo 'exists'
else
    echo 'NOT exists'
fi

如果[-d“~/sample”]
有什么问题吗?

是的,双引号是不允许~expand。。。以下工作将起作用:

if [ -d ~"/sample" ]; then
   echo "exists"
fi
通常最好使用:

if [ -d "$HOME/sample" ] ; then
   echo "exists"
fi
$HOME通常由Bourne shells设置