Save Bash Shell Script Output To a File

How do I save my bash shell script output to a file?

You can save bash shell script output to a file using the following syntax:
$ ./yourscript.sh > output.txt
$ /path/to/your/script.sh > output.txt

To append output to existing data.txt file, enter:
$ ./yourscript.sh >> data.txt
$ /path/to/your/script.sh >> data.txt

ADVERTISEMENTS

Understanding > and >> Redirection

Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment. The following redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.

Redirecting Output

Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size. The general format for redirecting output is:

[n]> output.txt
command> output.txt
script.sh> output.txt

If the redirection operator is >, and the noclobber option to the set builtin has been enabled, the redirection will fail if the file whose name results from the expansion of word exists and is a regular file. If the redirection operator is >|, or the redirection operator is > and the noclobber option to the set builtin command is not enabled, the redirection is attempted even if the file named by word exists.

Appending Redirected Output

Redirection of output in this fashion causes the file whose name results from the expansion of word to be opened for appending on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created. The general format for appending output is:

[n]>>append.txt
command>> append.txt
script.sh>> append.txt
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • Leon Sep 12, 2011 @ 4:05

    Thanks for the tute, was a good refresher

  • Andrew Oct 18, 2011 @ 12:30

    I’m a noob to the command line. Is there a way to run an interactive script and output to a file? In other words, can the outbut be directed to both the command line and a file?

    • Jerry Rice Nov 23, 2011 @ 8:52

      Andrew, yes see the command “tee”.

  • Yauvaraj Rimal Nov 5, 2012 @ 5:33

    What i want to know is, Is there any limit on line size of the output while writing output to file?

  • Yash Apr 6, 2016 @ 6:51

    Thanks.It helped me a lot.Such a simple issue.Because I used “<<" instead of "<" my data used to get appended into the output file.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.