开发者

Error when outputting to file name variable in shell script

I wrote a very simple program to test outputting data to a file name variable.

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

When I run this script, I got the following error

"./script.s开发者_开发技巧h: line 3: ~/output: No such file or directory"

So how should I revise my code to get it work? Or it's not supported in shell script?


The quotes around the "~/output" are causing you grief.

e.g

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

works ok.

To see what's going on, try

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

vs

$ file=~/output
$ echo $file

and bear in mind that ~ is a shell expansion for the home directory.


You can use the $HOME environment variable instead in a script:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜