Writing to a File in Bash
This article will let us explore different ways to write files in bash. Files can be appended or overwritten as required. Let's see how we can do it.
Different ways to write/overwrite files in Bash
We will see several operators, such as >
and >>
, for overwriting files and appending output to files. In addition, we will explore tee
the command for overwriting and appending single and multiple files.
Overwriting files using the > operator
>
Operator is used to overwrite a file. For example, the following code is used to overwrite a file.
echo "Overwriting in the file" > test.txt
The above command overwrites the test.txt file if it exits; otherwise, it creates a new one. echo is not usually used because it does not support text formatting.
We use printf
instead of printf for formatted text echo
. The following example shows the use of printf to overwrite a file.
printf "overwriting using the printf \n now in next line" > test.txt
The above command will format the string and printf
add a new line after .
Use the >> operator to write to a file
>>
Operator is used to append content at the end of a file. For example, the following command is used to append data to a file named test.txt.
echo "Append in the file" >> test.txt
Use vim command to write files
The vim editor is also used to edit the contents of a file. For example, the following code will open the test.sh file in the vim editor.
sudo vim test.sh
After opening the vim editor, add the following script to perform the file writing operation.
#! /bin/bash
echo "Enter Student Name"
read yourName
echo $yourName > test.txt
echo "Enter age"
read age
echo $age >> test.txt
cat test.txt
After adding the code, press ESC and type w to close the file. Now bash test.sh
run the file using .
The above script shows two methods of writing to a file. In line 4, we use >
the operator, which overwrites the file. In line 7, >>
the operator is used, which does not overwrite the previous content and appends the string to a new line.
Use tee command to write to a file
If we want to perform write operations to a file and the console at the same time, we can use tee
the command. It takes input and writes the string to the file and the console at the same time.
Using tee
the command, we can see in the console what is being written in the file. tee
The command is also used to overwrite and append files.
The following command is used to overwrite using tee command.
echo "Testing the Tee Command" | tee test.txt
The above command will |
write the string input from the pipe to the console and to the file at the same time.
The -a flag tee
is used with the test command to append a file. For example, the following code will append the test Tee command to the test.txt file.
echo "Testing the Tee Command" | tee -a test.txt
Sometimes, we may need to write the contents from multiple files at the same time. We can tee
do this using the command by specifying the names of all the files separated by a single space.
The following bash
command demonstrates writing content to multiple files:
echo "Writing text to the multiple files" | tee test1.txt test2.txt test3.txt
The above command writes the input string both in the console and in three text files.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Running Regular Expressions in Case Statements in Bash
Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM
-
This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters
Terminating a Process in Bash
Publish Date:2025/03/22 Views:135 Category:OPERATING SYSTEM
-
This article will first discuss the different concepts related to Linux processes. After this, we will learn the different ways to terminate a process. Before going into the kill command, we must understand some preliminary concepts. Simple
Solve R command not found on Bash (or Cygwin)
Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM
-
Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke
Error handling in Bash
Publish Date:2025/03/21 Views:94 Category:OPERATING SYSTEM
-
This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi
Getting the absolute path in Bash
Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM
-
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute
Difference between Bash Nohup and &
Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
Open Emacs in Bash
Publish Date:2025/03/21 Views:142 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your