Bash脚本:mkdir错误

时间:2012-09-15 12:29:22

标签: bash quotes sh mkdir

我有以下脚本示例:

#!/bin/bash

# Aborts the script on "simple command failure" (does not cover pipes)
set -e

# Makes sure we do not run the script outside the correct directory (i.e. the backup directory)
projects_directory='~/projects' 
backup_drectory="${projects_directory}/backup/"
echo "Backup directory: ${backup_drectory}"

if [ ! -d "$projects_directory" ]; then 
    mkdir "$projects_directory"
    echo "${projects_directory} created successfully" 
fi

以下输出失败了:

Backup directory: ~/projects/backup/
mkdir: cannot create directory `~/projects': No such file or directory

我不明白为什么。如果我在终端中手动输入mkdir ~/projects命令,则会创建目录。任何建议都是最受欢迎的。

1 个答案:

答案 0 :(得分:7)

删除单引号:

projects_directory=~/projects

引用可防止shell扩展~字符。