funwithlinux blog

How to Write Variable Contents to a File in Shell Scripting: Step-by-Step Guide

Shell scripting is a powerful tool for automating repetitive tasks, managing system configurations, and processing data in Unix-like environments (Linux, macOS, etc.). A common requirement in shell scripting is writing dynamic content—such as variables, user input, or computed values—to files. Whether you’re generating log files, creating configuration scripts, or storing output for later analysis, knowing how to write variable contents to a file is a fundamental skill.

This guide will walk you through step-by-step methods to write variables to files in shell scripting, explain common pitfalls, and share best practices to avoid errors. By the end, you’ll be able to confidently integrate file writing into your shell scripts.

2026-01

Table of Contents#

  1. Prerequisites
  2. Understanding Variables in Shell Scripting
  3. Methods to Write Variables to a File
  4. Step-by-Step Examples
  5. Common Pitfalls and Solutions
  6. Best Practices
  7. Conclusion
  8. References

Prerequisites#

Before diving in, ensure you have:

  • A Unix-like terminal (Linux, macOS, or WSL on Windows).
  • Basic familiarity with shell scripting (e.g., running scripts, using bash).
  • Understanding of file permissions (e.g., chmod to make scripts executable).

Understanding Variables in Shell Scripting#

Variables in shell scripting store data (text, numbers, or command output) that can be reused throughout a script. They are declared with the syntax variable_name=value (no spaces around =).

Key Rules for Variables:#

  • No spaces: name="John Doe" is valid; name = "John Doe" is not.
  • Quoting: Use double quotes (" ") to preserve spaces and allow variable expansion (e.g., "Hello $name"). Single quotes (' ') treat text literally (e.g., 'Hello $name' prints $name as-is).
  • Expansion: Variables are referenced with $variable_name (e.g., echo $name).

Example Variable Declarations:#

# String variable (with spaces, so use double quotes)
user_name="Alice Smith"
 
# Numeric variable
user_age=30
 
# Variable with command output (use $(...) for command substitution)
current_date=$(date "+%Y-%m-%d")
 
# Multi-line variable (use \n for newlines; we’ll handle this later)
user_bio="Name: $user_name\nAge: $user_age\nJoined: $current_date"

Methods to Write Variables to a File#

There are several ways to write variables to a file in shell scripting. We’ll cover the most common methods, their use cases, and syntax.

Method 1: Using echo Command#

The echo command is the simplest way to print text (or variables) to the terminal or a file. To write variables to a file, combine echo with redirection operators (> or >>).

Redirection Operators:#

  • >: Overwrites the file (creates the file if it doesn’t exist).
  • >>: Appends to the file (creates the file if it doesn’t exist).

Syntax:#

echo "$variable_name" > output_file.txt   # Overwrite file
echo "$variable_name" >> output_file.txt  # Append to file

Example:#

user_name="Alice"
echo "Hello, $user_name!" > greeting.txt  # Writes "Hello, Alice!" to greeting.txt

Method 2: Using printf Command#

The printf command offers more control over formatting (e.g., adding newlines, padding, or numeric formatting) compared to echo. It’s especially useful for structured output (e.g., tables, config files).

Syntax:#

printf "Format string" "$var1" "$var2" > output_file.txt
  • The "format string" uses placeholders like %s (string), %d (integer), or \n (newline).

Example:#

name="Bob"
score=95
printf "Name: %s\nScore: %d\n" "$name" "$score" > student_results.txt

Output in student_results.txt:

Name: Bob
Score: 95

Method 3: Using cat with Here-Documents#

A "here-document" (here-doc) lets you embed multi-line text directly in a script. Combined with cat, it’s ideal for writing large blocks of text (e.g., config files) that include variables.

Syntax:#

cat << EOF > output_file.txt
Line 1: $var1
Line 2: $var2
Multi-line content with $var3
EOF
  • << EOF starts the here-doc; EOF (or any delimiter) ends it.
  • Variables inside the here-doc expand (use << 'EOF' to disable expansion).

Example:#

app_name="MyApp"
version="2.1.0"
author="Alice"
 
cat << EOF > app_config.txt
[AppInfo]
Name = $app_name
Version = $version
Author = $author
LastUpdated = $(date)
EOF

Output in app_config.txt:

[AppInfo]
Name = MyApp
Version = 2.1.0
Author = Alice
LastUpdated = Wed Oct 11 14:30:00 2023

Method 4: Using tee Command#

The tee command writes input to both a file and the terminal (stdout). It’s useful for logging (e.g., saving output while still seeing it on the screen).

Syntax:#

echo "$variable" | tee output_file.txt   # Overwrite file
echo "$variable" | tee -a output_file.txt  # Append to file (use -a flag)

Example:#

status="Backup completed successfully"
echo "$status" | tee backup_log.txt  # Writes to backup_log.txt and prints to terminal

Step-by-Step Examples#

Let’s apply these methods to real-world scenarios.

Example 1: Basic Variable Writing#

Goal: Write a user’s name and age to a file.

  1. Declare variables:

    name="Charlie Brown"
    age=25
  2. Use echo to write variables to user_info.txt:

    echo "Name: $name" > user_info.txt
    echo "Age: $age" >> user_info.txt  # Append the second line
  3. Verify the file:

    cat user_info.txt

    Output:

    Name: Charlie Brown
    Age: 25
    

Example 2: Appending Variables to a File#

Goal: Log user login times to a login_log.txt file (append, don’t overwrite).

  1. Declare variables with dynamic data (current time):

    user="diana"
    login_time=$(date "+%Y-%m-%d %H:%M:%S")  # Format: 2023-10-11 15:45:30
  2. Append to the log file:

    echo "User $user logged in at $login_time" >> login_log.txt
  3. Run the script multiple times; the log will grow:

    User diana logged in at 2023-10-11 15:45:30
    User diana logged in at 2023-10-11 16:00:15
    

Example 3: Writing Multi-Line Variables#

Goal: Write a multi-paragraph bio to a file using a single variable.

  1. Declare a multi-line variable (use \n for newlines):

    bio="Hello! I'm $name.\nI'm a developer with 5 years of experience.\nFavorite language: Bash."
  2. Use printf to handle newlines (more reliable than echo -e):

    printf "%b" "$bio" > bio.txt  # %b expands escape sequences like \n
  3. Output in bio.txt:

    Hello! I'm Charlie Brown.
    I'm a developer with 5 years of experience.
    Favorite language: Bash.
    

Example 4: Generating a Config File#

Goal: Automatically generate a Nginx server config file with variables for domain and port.

  1. Declare variables for the config:

    domain="example.com"
    port=8080
    root_dir="/var/www/$domain"
  2. Use a here-doc with cat to write the config:

    cat << EOF > /etc/nginx/sites-available/$domain.conf
    server {
        listen $port;
        server_name $domain www.$domain;
        root $root_dir;
        index index.html;
     
        location / {
            try_files \$uri \$uri/ =404;  # \$uri is escaped to avoid variable expansion
        }
    }
    EOF
  3. The generated example.com.conf file will include your dynamic values!

Common Pitfalls and Solutions#

Even experienced scripters hit these issues. Here’s how to avoid them:

1. Variable Expansion Fails in Single Quotes#

Problem: Variables inside single quotes (' ') are not expanded.
Example:

name="Alice"
echo 'Hello $name' > file.txt  # Writes "Hello $name" instead of "Hello Alice"

Solution: Use double quotes (" ") to expand variables:

echo "Hello $name" > file.txt  # Correct: Writes "Hello Alice"

2. Accidentally Overwriting Files#

Problem: Using > instead of >> erases existing file content.
Example:

echo "First line" > data.txt
echo "Second line" > data.txt  # Overwrites! Now data.txt has only "Second line"

Solution: Use >> to append:

echo "First line" > data.txt
echo "Second line" >> data.txt  # Appends! Now data.txt has both lines

3. Word Splitting (Spaces in Variables)#

Problem: Variables with spaces split into multiple words, breaking output.
Example:

message="Hello world"
echo $message > file.txt  # Works here, but...
echo $message | wc -w     # Output: 2 (correct), but risky in complex scripts

Solution: Always quote variables to preserve spaces:

echo "$message" > file.txt  # Safest practice

4. Permission Denied Errors#

Problem: Trying to write to a protected directory (e.g., /root/ without sudo).
Example:

echo "data" > /root/file.txt  # Error: Permission denied

Solution: Check file permissions with ls -l, or run the script with sudo (if trusted).

5. Special Characters Breaking Output#

Problem: Variables with newlines, quotes, or backslashes cause unexpected formatting.
Example:

description="He said, "Hello!""  # Syntax error (unclosed quote)

Solution: Escape quotes with \ or use single quotes for the variable:

description='He said, "Hello!"'  # Single quotes preserve internal quotes
echo "$description" > file.txt   # Writes: He said, "Hello!"

Best Practices#

Follow these tips to write robust, maintainable scripts:

  1. Use Meaningful Variable Names: Avoid var1 or x; use user_name or backup_path.
  2. Quote Variables: Always wrap variables in double quotes ("$var") to prevent word splitting.
  3. Test with echo First: Before writing to a file, test variable expansion with echo "$var" to verify output.
  4. Comment Your Code: Explain why you’re writing variables to a file (e.g., # Log user activity to audit log).
  5. Handle Errors: Check if a file is writable before writing:
    if [ -w "output.txt" ]; then
        echo "$var" > output.txt
    else
        echo "Error: Cannot write to output.txt" >&2  # Send error to stderr
        exit 1
    fi
  6. Avoid Cluttering Files: Use >> sparingly; rotate logs or clean up old files if needed.

Conclusion#

Writing variable contents to files is a cornerstone of shell scripting, enabling automation of config files, logs, reports, and more. By mastering echo, printf, here-documents, and tee, you can handle everything from simple variable writes to complex multi-line configurations.

Remember to quote variables, avoid overwriting files accidentally, and test thoroughly. With these skills, you’ll build scripts that are dynamic, reliable, and easy to maintain.

References#

Let me know if you have any questions—happy scripting! 🚀