As far as I know there is no prepend operator on a bash or any other shell, however there are many ways to do the same. You can use ed, sed, perl, awk and so on to add text to the beginning of a file in Bash under Linux or Unix-like systems.
I am a new Unix system user with a bash as a default shell. I can append text to a file using >> operator but how do I prepend a text to a file? I want the opposite operation of >> . How can I prepend some data to a text file? How do we add text to the beginning of a file in Bash under Linux?
ADVERTISEMENTS
As far as I know there is no prepend operator on a bash or any other shell, however there are many ways to do the same. You can use ed, sed, perl, awk and so on to add text to the beginning of a file in Bash under Linux or Unix-like systems.
Here is simple solution using a temporary file to prepend text:
echo 'line 1' > /tmp/newfile
echo 'line 2' >> /tmp/newfile
cat yourfile >> /tmp/newfile
cp /tmp/newfile yourfile
Here is one line solution:
echo "text"|cat - yourfile > /tmp/out && mv /tmp/out yourfile
echo "nixCraft"|cat - yourfile > /tmp/out && mv /tmp/out yourfile
Prepending A Text or Lines To a File Under Linux and Unix
No need to create a temp file. The syntax is:
echo -e "DATA-Line-1\n$(cat input)" > input
cat input
To add multiple lines:
echo -e "DATA-Line-1\nDATA-Line-2\n$(cat input)" > input
cat input
For example add text to the beginning of a text file called input using bash as follows:
cat input
echo -e "Famous Quotes\n$(cat input)" > input
The syntax is follows to prepend A text or lines to a file when we use the sed command:
sed '1s;^;DATA-Line-1\n;' input > output
### GNU/sed syntax ##
sed -i '1s;^;DATA-Line-1\n;' input
### Verify it using the cat ##
cat input
Here is a sample input file:
$ cat input.txt
This is a test file.
I love Unix.
Next prepend two lines (make sure you add \n):
$ sed -i '1s;^;Force-1\nForce-2\n;' input.txt
$ cat input.txt
Force-1
Force-2
This is a test file.
I love Unix.
The awk command syntax is:
awk '{print "Line-1" $0}' file
### add a new line for each matched line ##
awk '{print "~~~~~~~~\n" $0}' quotes.txt > output.txt
Display result using the [nicmd name=”cat”] or grep command/egrep command:
$ cat output.txt
The sed syntax is:
sed -i -e 's/^/Line-1/' file
Looking to develop real-time applications? **[Hire Dedicated Linux Developer](https://hourlydeveloper.io/hire-dedicated-linux-developer/ "Hire Dedicated Linux Developer")** from **[HourlyDeveloper.io](https://hourlydeveloper.io/...
This article is all about my journey on switching from Windows 10 to Linux Mint 20, how I got easily adapted to the Linux environment, and some resources that helped me to set up a perfect Desktop environment.
User Administration in Linux - Linux Tutorial - Linux Training
Package Management in Linux - Linux Tutorial - Linux Training
Ubuntu is arguably one of the most popular and widely-used Linux distribution owing to its classic UI, stability, user-friendliness, and a rich repository that contains over 50,000 software packages. Furthermore, it comes highly recommended for beginners who are trying to give a shot at Linux.