How can I write a here document to a file in Bash script?
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
- Spread 'sed' command over multiple lines
To build on @Livven's answer, here are some useful combinations.
variable substitution, leading tab retained, overwrite file, echo to stdout
no variable substitution, leading tab retained, overwrite file, echo to stdout
variable substitution, leading tab removed, overwrite file, echo to stdout
variable substitution, leading tab retained, append to file, echo to stdout
variable substitution, leading tab retained, overwrite file, no echo to stdout
the above can be combined with
sudo
as wellAs instance you could use it:
First(making ssh connection):
Second(executing commands):
Third(executing commands):
Forth(using variables):
Note:
The question (how to write a here document (aka heredoc) to a file in a bash script?) has (at least) 3 main independent dimensions or subquestions:
root
) own the file?(There are other dimensions/subquestions which I don't consider important. Consider editing this answer to add them!) Here are some of the more important combinations of the dimensions of the question listed above, with various different delimiting identifiers--there's nothing sacred about
EOF
, just make sure that the string you use as your delimiting identifier does not occur inside your heredoc:To overwrite an existing file (or write to a new file) that you own, substituting variable references inside the heredoc:
To append an existing file (or write to a new file) that you own, substituting variable references inside the heredoc:
To overwrite an existing file (or write to a new file) that you own, with the literal contents of the heredoc:
To append an existing file (or write to a new file) that you own, with the literal contents of the heredoc:
To overwrite an existing file (or write to a new file) owned by root, substituting variable references inside the heredoc:
To append an existing file (or write to a new file) owned by user=foo, with the literal contents of the heredoc:
I like this method for concision, readability and presentation in an indented script:
Where
→
is a tab character.When root permissions are required
When root permissions are required for the destination file, use
|sudo tee
instead of>
:Instead of using
cat
and I/O redirection it might be useful to usetee
instead:It's more concise, plus unlike the redirect operator it can be combined with
sudo
if you need to write to files with root permissions.