1

我有以下脚本示例:

#!/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在终端中手动输入命令,则会创建目录。任何建议都是最受欢迎的。

4

1 回答 1

7

删除单引号:

projects_directory=~/projects

引用防止外壳扩展~字符。

于 2012-09-15T12:34:34.643 回答