I have a file called .env.

Inside this file, I have the variable MYIP= and in front of MYIP= I need to put my public IP.

I have this public in inside another file called publicip.

I want to use the output of the publicip file and append in front of MYIP=.

I tried remove MYIP= and use the command:

echo "MYIP=" | cat - publicip >> .env

But inside the file I see the information: MYIP= 177.XX.XX.XX

It's not in the same line. Can anyone help me?

  • 1
    Why would you use cat for this? Cat is not the right tool for the job at all.
    – terdon
    Commented Aug 6, 2020 at 22:11
  • ... +1 sounds more like a job for paste Commented Aug 6, 2020 at 22:21

2 Answers 2


There are many ways to do this. cat is one of the less common choices but still totally workable. You almost have it but echo comes with a line break. You can suppress it with the -n flag:

echo -n "MYIP=" | cat - publicip >> .env

Assuming the .env file contains other content that you want to keep, one way would be to simply replace the MYIP assignment with perl:

perl -pe 's/MYIP=.*/MYIP='`cat publicip`'/' -i .env

In all similar solutions using text tools (perl, cat, sed, awk) beware that it's very easy to break them with malformed publicip, so make sure you are in full control of this file. Use such one-liners with care in safe environment, otherwise they are almost impossible to secure properly and things can go wild.

One way of such security precaution would be to not modify .env file in place, instead use some templates and merge/substitute them to create output file. This won't help you with malicious actions nor mistakes (or conditions like no free space resulting in truncated file), but at least you won't lose the template file contents so you'll be able to restore .env.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .