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

twi*_*imo 4 unix scripting

我写了一个非常简单的程序来测试输出数据到文件名变量.

#!/bin/sh
file="~/output"
echo "test" > $file
Run Code Online (Sandbox Code Playgroud)

当我运行此脚本时,我收到以下错误

"./script.sh:line 3:〜/ output:没有这样的文件或目录"

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

Bri*_*new 5

"〜/输出"周围的引号让你感到悲痛.

例如

#!/bin/sh
file=~/output
echo "test" > $file
Run Code Online (Sandbox Code Playgroud)

工作正常.

要了解发生了什么,请尝试

$ file="~/output"
$ echo $file
Run Code Online (Sandbox Code Playgroud)

VS

$ file=~/output
$ echo $file
Run Code Online (Sandbox Code Playgroud)

并记住〜是主目录的shell扩展.