This method helps you bypass the need to issue multiple commands when writing multiple lines to a file.īelow is an example of a heredoc redirect that writes multiple lines to a file. Here Document (Heredoc) redirection allows you to pass multiline input to a shell command.
echo 'This text is appended to the file.' > example-double-arrow.txt echo 'This text is written to a file.' > example-double-arrow.txtĪppend additional text to the end of the file. Write text to a new file, creating the file in the process. However, the important distinction is that > appends contents to an already existing file rather than overwriting them. It writes text you pass to it to a designated file, creating the file if it does not already exist. The append operator ( >) works much like the regular output operator. This text overwrites the file's contents. echo 'This text overwrites the file's contents.' > example-single-arrow.txt Write text to the same file again, and observe that the previous contents are overwritten. You can verify the file’s contents using the cat command. echo 'This text is written to a file.' > example-single-arrow.txt The command creates the file in the process. Issue the following command to write the text to a file. Using the > operator on an existing file overwrites that file’s contents.įollow the steps below to learn how to use the > operator: