Bash Write Variable to File

Bash write variable to file

Using echo Command

Use the echo command with redirection operator (>) to write variable to file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
echo $greetings > results.txt
cat results.txt
Welcome to Java2Blog!

Use the echo command with the append operator (>>) to append a variable to the end of the specified file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
echo $greetings >> results.txt
cat results.txt
Welcome to Java2Blog!
Welcome to Java2Blog!

Use the echo command with the append operator (>>) to append a variable’s content with a custom string to the end of the file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
echo "$greetings Let's learn" >> results.txt
cat results.txt
Welcome to Java2Blog!
Welcome to Java2Blog!
Welcome to Java2Blog! Let's learn

In the above examples, we declared and initialized the greetings variable and set its value to a string type value. Then, we used the echo command with a redirect (>) and append (>>) operators to write the content of the $greeting variable to the output file (results.txt).

If you use the > operator when you already have content in your output file, the redirect operator will overwrite the content. In that case, the append operator represented by >> should be preferred not to lose the content. Remember, you would not get any error if the output file does not exist but would be created and written.

By default, the echo command adds a newline at the end.

Using printf Statement

Use the printf statement with redirection operator (>) to write variable to file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
printf "%s" "$greetings" > output.txt
cat output.txt
Welcome to Java2Blog!

Use the printf statement with the append operator (>>) to append a variable’s content to the end of the specified file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
printf "%s" "$greetings" >> output.txt
cat output.txt
Welcome to Java2Blog!Welcome to Java2Blog!

Use the printf statement with the append operator (>>) to append a variable’s content with a custom string to the end of the file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
printf "\n%s Let's start learning.\n" "$greetings" >> output.txt
cat output.txt
Welcome to Java2Blog!Welcome to Java2Blog!
Welcome to Java2Blog! Let's start learning.

These examples are similar to the ones learned in the previous section, but we used the printf statement with a redirect (>) and append (>>) operators to write the content of the $greeting variable to the output file (output.txt).

In the printf statement, the "%s" was a placeholder representing a string and telling the printf to expect a string value to be passed. Here, we passed the string value of the $greetings variable. This approach also creates the output file if it does not exist already.

By default, the printf command does not add a newline at the end. However, if you need to write on a new line every time, then use it as printf "%s\n" "$greetings" >> output.txt. Here, \n will add a newline after writing the provided variable’s value.

Using Here String Operator

Use a here string with redirection operator (>) to write the variable’s value to a given file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
cat <<<  $greetings > outputFile.txt
cat outputFile.txt
Welcome to Java2Blog!

Use a here string with append operator (>>) to append the variable’s value to the end of the specified text file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
cat <<<  $greetings >> outputFile.txt
cat outputFile.txt
Welcome to Java2Blog!
Welcome to Java2Blog!

Use a here string with append operator (>>) to append the variable’s value with a custom string to the end of the given text file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
cat <<<  "$greetings Let's start learning." >> outputFile.txt
cat outputFile.txt
Welcome to Java2Blog!
Welcome to Java2Blog!
Welcome to Java2Blog! Let's start learning.

Again, we initialized the greetings variable with a string value we wanted to write in the given file. Then, we used here string (<<) and append (>>) operators to write the greeting variable’s content to the output file (outputFile.txt).

Usually, we prefer the here string when we have to concatenate a custom string with the given variable’s value and then write the resulting value to the output file. Note that the here string operator (<< By default, the here string adds a newline character at the end of the line.

Using Here Document Operator

Use a here document with redirection operator (>) to write the variable’s value to the given file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
cat << EOF > outcome.txt
$greetings
EOF
cat outcome.txt
Welcome to Java2Blog!

Use a here document with the append operator (>>) to append the variable’s value to the end of a given file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
cat << EOF >> outcome.txt
$greetings
EOF
cat outcome.txt
Welcome to Java2Blog!
Welcome to Java2Blog!

Use a here document with the append operator (>>) to append the variable’s value with custom text to the end of the file in Bash.

#!/bin/bash
greetings="Welcome to Java2Blog!"
cat << EOF >> outcome.txt
$greetings Let's start learning.
EOF
cat outcome.txt
Welcome to Java2Blog!
Welcome to Java2Blog!
Welcome to Java2Blog! Let's start learning.

Here, we used here document to write the variable’s content to the specified output file (outcome.txt). If the outcome.txt file was already present, the > operator would overwrite the content, while >> would append. On the other hand, the outcome.txt file would be created if it was not already there.

In the above examples, we used the cat command with the here document to provide the content that would be written to the outcome.txt file. Here, the << was called a here document operator, allowing for multiline input without explicitly specifying the input source (e.g., file etc.).

Next, the EOF was a delimiter that marked the start of the here document (`< By default, the here document adds a newline character at the end of the line.

That’s all about Bash write variable to file.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.