Error while outputting a file name in a shell script variable

I wrote a very simple program to check the output of data into a file name variable.

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

When I run this script, I got the following error

"./ script.sh: line 3: ~ / output: no such file or directory"

So, how do I review my code to make it work? Or is it not supported in a shell script?

+4
source share
2 answers

The quotes around "~ / output" make you sad.

eg

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

works fine.

To find out what is going on, try

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

vs

 $ file=~/output $ echo $file 

and remember that ~ is a shell extension for the home directory.

+5
source

Instead of the $ HOME variable, you can use the script variable:

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

Source: https://habr.com/ru/post/1337221/


All Articles