I would like to copy the contents of a variable (here called var
) into a file.
The name of the file is stored in another variable destfile
.
I'm having problems doing this. Here's what I've tried:
cp $var $destfile
I've also tried the same thing with the dd command... Obviously the shell thought that $var
was referring to a directory and so told me that the directory could not be found.
How do I get around this?
None of the answers above work if your variable:
-e
-n
-E
\
followed by an n
and so they cannot be relied upon for arbitrary string contents.
In bash, you can use "here strings" as:
cat <<< "$var" > "$destdir"
As noted in the comment below, @Trebawa's answer (formulated in the same room as mine!) using printf
is a better approach.
When you say "copy the contents of a variable", does that variable contain a file name, or does it contain a name of a file?
I'm assuming by your question that $var
contains the contents you want to copy into the file:
$ echo "$var" > "$destdir"
This will echo the value of $var into a file called $destdir. Note the quotes. Very important to have "$var" enclosed in quotes. Also for "$destdir" if there's a space in the name. To append it:
$ echo "$var" >> "$destdir"
echo
has the problem that if var
contains something like -e
, it will be interpreted as a flag. Another option is printf
, but printf "$var" > "$destdir"
will expand any escaped characters in the variable, so if the variable contains backslashes the file contents won't match. However, because printf
only interprets backslashes as escapes in the format string, you can use the %s
format specifier to store the exact variable contents to the destination file:
printf "%s" "$var" > "$destdir"
All of the above work, but also have to work around a problem (escapes and special characters) that doesn't need to occur in the first place: Special characters when the variable is expanded by the shell. Just don't do that (variable expansion) in the first place. Use the variable directly, without expansion.
Also, if your variable contains a secret and you want to copy that secret into a file, you might want to not have expansion in the command line as tracing/command echo of the shell commands might reveal the secret. Means, all answers which use $var
in the command line may have a potential security risk by exposing the variable contents to tracing and logging of the shell.
Use this:
printenv var >file
That means, in case of the OP question:
printenv var >"$destfile"
Note: variable names are case sensitive.
If I understood you right, you want to copy $var
in a file (if it's a string).
echo $var > $destdir
Source: Stackoverflow.com