JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Writing to a File in Bash

Author:JIYIK Last Updated:2025/03/22 Views:

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 teethe 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 printfinstead 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 printfadd 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.shrun 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 teethe command. It takes input and writes the string to the file and the console at the same time.

Using teethe command, we can see in the console what is being written in the file. teeThe 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 teeis 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 teedo this using the command by specifying the names of all the files separated by a single space.

The following bashcommand 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.

Article URL:

Related Articles

Move Multiple Files in Linux Bash

Publish Date:2025/04/06 Views:97 Category:OPERATING SYSTEM

In this article, we will explain how to move multiple files to the same directory in Linux. We will explain different methods such as typing multiple file names, using wildcard characters ( * ) to represent similar file names and/or identic

How to Compare Strings in Bash

Publish Date:2025/04/06 Views:190 Category:OPERATING SYSTEM

We can compare strings using various comparison operators and check if a string contains a substring using regular expressions. String comparison in Bash String comparison means checking whether the given strings are identical or not. Two o

How to Add Comments in Bash Scripts

Publish Date:2025/04/06 Views:123 Category:OPERATING SYSTEM

Comments are lines that are ignored by the interpreter and are only used to describe what is happening in the code or to provide insight into a particular block or line of code. Comments make it easier for the reader to understand the code.

tr Command in Linux Bash

Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM

In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma

Differences between Bash Profile and Bashrc

Publish Date:2025/04/05 Views:188 Category:OPERATING SYSTEM

This article explains the difference between ~./bash_profile and files in Bash. ~/.bashrc What are startup files in Bash? Startup files are files that are executed after the shell is started. The startup files depend on the type of shell th

Differences between Sh and Bash

Publish Date:2025/04/05 Views:145 Category:OPERATING SYSTEM

This article explains what a shell is, find out which shell you are currently using, check a list of all available shells, and the difference between sh and . bash What is a Shell A shell is a computer program that accepts commands. It also

How to Append Text to a File Using Bash

Publish Date:2025/04/05 Views:83 Category:OPERATING SYSTEM

We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th

How to Concatenate Strings in Bash

Publish Date:2025/04/05 Views:65 Category:OPERATING SYSTEM

String concatenation is one of the most widely used operations in programming. It refers to connecting two or more strings by putting one string at the end of another string. To concatenate strings in Bash, we can write string variables one

Configure Git Bash with Visual Studio Code

Publish Date:2025/03/31 Views:138 Category:Git

This article outlines the steps to configure Git Bash with Visual Studio Code on Windows. By default, VSCode uses PowerShell as the integrated terminal. We can configure VSCode to use Git Bash as the integrated terminal. Make sure you have

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial