Manipulating file content is a routine task in Shell Scripting. This article demonstrates how to add text to a file, creating the file if it doesn’t already exist. This skill is particularly useful for logging, file modification, and simple data appending tasks.
Create a new script file:
Open a text editor like nano
or vi
to start a new script file. Type the following lines in the script:
Save and Close: In nano
, save your script by pressing CTRL+X
, then Y
, followed by Enter
.
Making the script executable
Change the script’s permissions to make it executable:
Run your script using:
To check if “Freshers.in!” has been added to ‘hello.txt’, use: