Shell - Write variable contents to a file

LinuxBashShell

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

Use the echo command:

var="text to append";
destdir=/some/directory/path/filename

if [ -f "$destdir" ]
then 
    echo "$var" > "$destdir"
fi

The if tests that $destdir represents a file.

The > appends the text after truncating the file. If you only want to append the text in $var to the file existing contents, then use >> instead:

echo "$var" >> "$destdir"

The cp command is used for copying files (to files), not for writing text to a file.

Solution 2 - Linux

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"

Solution 3 - Linux

None of the answers above work if your variable:

  • starts with -e
  • starts with -n
  • starts with -E
  • contains a \ followed by an n
  • should not have an extra newline appended after it

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 by Ash below, @Trebawa's answer (formulated in the same room as mine!) using printf is a better approach than cat.

Solution 4 - Linux

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.

For variables that are already exported, use this:

printenv var >file

That means, in case of the OP question:

printenv var >"$destfile"

Note: variable names are case sensitive.

Warning: It is not a good idea to export a variable just for the sake of printing it with printenv. If you have a non-exported script variable that contains a secret, exporting it will expose it to all future child processes (unless unexported, for example using export -n).

Solution 5 - Linux

If I understood you right, you want to copy $var in a file (if it's a string).

echo $var > $destdir

Solution 6 - Linux

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"

Solution 7 - Linux

you may need to edit a conf file in a build process:

echo "db-url-host=$POSTGRESQL_HOST" >> my-service.conf

You can test this solution with running before export POSTGRESQL_HOST="localhost"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1546083View Question on Stackoverflow
Solution 1 - Linuxpb2qView Answer on Stackoverflow
Solution 2 - LinuxTrebawaView Answer on Stackoverflow
Solution 3 - LinuxEricView Answer on Stackoverflow
Solution 4 - LinuxChristian HujerView Answer on Stackoverflow
Solution 5 - LinuxqwertzView Answer on Stackoverflow
Solution 6 - LinuxDavid W.View Answer on Stackoverflow
Solution 7 - LinuxThykofView Answer on Stackoverflow