Creating a file with some content in Shell scripting
Just use output redirection. E.g.
#!/bin/bash echo "some file content" > /path/to/outputfile
>will write all
stdinprovided by the
echoto the file
Alternatively, you could also use
teeand a pipe for this. E.g.
echo "some file content" | tee outputfile
Be aware that any of the examples will overwrite an existing
If you need to append to a currently existing file, use
If you don't accept user input in this line, no user input can change the behaviour here.
I think it is superior to use a here doc to create a new file in a script. It is cleaner looking, which I believe encourages readability.
cat > filename <<- "EOF" file contents more contents EOF
<<-is optional, allowing for tabbed indents which will be stripped when the file is created. The quotes around the
"EOF"prevent the "here doc" from doing any substitutions.