Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Sometimes, when writing scripts in Linux, we may want to write the contents of a Bash variable to a file. In this tutorial, we’ll present a few ways to write information stored in Bash variables to a file.

2. Using echo

One way we can write variable contents to a file is to use the echo command along with the redirect operator.

Let’s start with a simple Bash command:

$ my_var="some value"
$ echo "$my_var" > file.log
$ cat file.log

This creates a variable $my_var, writes it to a file file.log using echo, and then outputs the file’s contents for inspection:

some value

In the echo command, we use the double quotations around $my_var to avoid a case where echo interprets the string contents as arguments. For example, let’s run:

$ my_var="-e some value"
$ echo $my_var > file.log
$ cat file.log

We see the following output:

some value

In this case, the -e argument applies to the call to echo and is not sent as output to the file. So we only see “some value”.

Another thing we should note is that, by default, echo puts a newline character at the end of the input.

3. Using printf

As we saw above, in some cases using echo will not work for certain text input. Another option is using printf. This provides more formatting options for our string. Another advantage is that printf returns a non-zero exit code if there is an issue writing the variable to the file, whereas echo always returns 0.

Changing our commands from above:

$ my_var="some value"
$ printf "$my_var" > file.log
$ cat file.log

We can see the following output:

some value$

However, if we want this output to match the echo output, we need to add \n after $myvar. This is because printf doesn’t add in the newline by default. After we add the newline, the variable content is on its own line:

$ my_var="some value"
$ printf "$my_var\n" > file.log
$ cat file.log
some value
$

4. here Strings

We can also use here string to write variable contents to a file. It allows for the redirection of a string to a command. This has the advantage of handling any arbitrary characters that may be in the variable.

Let’s try it out:

$ my_var="some value -e \n"
$ cat <<< "my variable is $my_var" > file.log
$ cat file.log

Here, we set $my_var to a string. Using here string, we then send a custom string containing the variable to cat. Third, we direct the output of cat to the output file via >. Finally, we use cat again to see the full contents of the variable in the file:

my variable is some value -e \n

5. Conclusion

In this article, we’ve shown a few ways to write the contents of a Bash variable to a file. Each method handles the contents of the variable differently, so we should always pick the one that fits our use case best.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!