在shell脚本中输出到文件名变量时出错

问题描述:

我写了一个非常简单的程序来测试将数据输出到文件名变量。在shell脚本中输出到文件名变量时出错

#!/bin/sh 
file="~/output" 
echo "test" > $file 

当我运行该脚本,我得到了以下错误

“./script.sh:3号线:〜/输出:没有这样的文件或目录”

所以我应该如何修改我的代码以使其工作?或者它在shell脚本中不受支持?

围绕“〜/输出”的报价是给你造成的悲伤。

e.g

#!/bin/sh 
file=~/output 
echo "test" > $file 

工作正常。

要看看发生了什么事情,尽量

$ file="~/output" 
$ echo $file 

VS

记住〜是主目录壳扩张
$ file=~/output 
$ echo $file 

和熊。

可以使用$ HOME环境变量,而不是一个脚本:

#!/bin/sh 
file="$HOME/output" 
echo "test" > $file