Writing output to a file is a basic and important skill in Bash scripting. In Linux, many tasks produce output in the terminal, but sometimes you need to save that output for later use. Bash allows you to write text, command results, logs, reports, and script output directly into files.
If you are learning Bash scripting, understanding how to write to a file is very useful. It helps in automation, log creation, backup scripts, system reports, cybersecurity monitoring, and error tracking. For example, you can save scan results, failed login attempts, system information, or backup logs into a file.
The > operator is used to write output to a file. If the file does not exist, Bash creates it. If the file already exists, Bash overwrites it.
Example:
echo "Welcome to Bash scripting" > output.txt
Check the file content:
cat output.txt
Output:
Welcome to Bash scripting
Be careful with > because it replaces old content.
The >> operator is used to add new content to the end of a file without deleting existing content.
Example:
echo "This is a new line" >> output.txt
Check the file:
cat output.txt
Output:
Welcome to Bash scriptingThis is a new line
Use >> when you want to keep old data and add new information.
You can save the output of Linux commands into a file.
Example:
whoami > user.txt
Save system information:
uname -a > system-info.txt
Save current date:
date > date.txt
This is useful when creating system reports.
You can use multiple echo commands:
#!/bin/bashecho "System Report" > report.txtecho "User: $(whoami)" >> report.txtecho "Hostname: $(hostname)" >> report.txtecho "Date: $(date)" >> report.txt
Save it as:
nano write-report.sh
Run it:
chmod +x write-report.sh./write-report.sh
View the report:
cat report.txt
A here document is useful for writing multiple lines clearly.
#!/bin/bashcat > message.txt << EOFHello,This file was created using a Bash script.Bash scripting is useful for Linux automation.EOF
This creates message.txt with multiple lines.
You can save failed SSH login attempts to a file:
#!/bin/bashgrep "Failed password" /var/log/auth.log > failed-ssh-logins.txtecho "Failed SSH login report saved."
This is useful for basic log analysis and security monitoring.
Writing to a file in Bash is simple and powerful. You can use > to overwrite a file and >> to append content. You can also save command output, create reports, write logs, and generate files using Bash scripts.
For beginners, learning how to write output to files is important because many real-world Bash scripts depend on logging and reporting. This skill is useful for Linux administration, automation, backups, and cybersecurity scripting.
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…
Introduction Working with files and directories is one of the most important skills in Bash…