shell脚本中ls、cd等命令不能识别函数传递的变量?

脚本如下:
aaa

#!/bin/bash

view()
{
ls $1
ls $dir
ls  ~/bakup/$new
}


read -p "选择目录的名称:" new
if [ -e ~/bakup/$new  ]
then
echo 
dir="~/bakup/$new"
view  $dir
fi

执行命令 sh aaa
输入 20151215
报错如下
ls: cannot access ~/bakup/20151215: No such file or directory
ls: cannot access ~/bakup/20151215: No such file or directory
wb

只有第三种方式显示正常,是什么原因呢?

阅读 6.4k
1 个回答

""中的~不会被展開成$HOME

推荐问题