Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
How to edit a file saved in string using sed?

I need to edit the file saved in a string using sed. As per the below example, I want to change some pattern in the php.ini file. Below example will allow only to change what is saved in the string.

[root@server ~]# PHPINI=/etc/php.ini
[root@server ~]# sed "s/somepattern/changedpattern/" <<< "$PHPINI"

Can somebody help? Thanks in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're using the herestring syntax which passes a string on standard input. You should instead pass the name of the file as an argument:

sed 's/somepattern/changedpattern/' /etc/php.ini

To overwrite the existing file, the standard method is to write to a temporary file and then overwrite the original:

sed 's/somepattern/changedpattern/' /etc/php.ini > tmp && mv tmp /etc/php.ini

Some versions of sed support "in-place" editing (which does more or less the same in the background):

sed -i.bak 's/somepattern/changedpattern/' /etc/php.ini

This creates a backup of the original file with a .bak suffix.

If the filename is contained within a variable, then simply use that instead

php_ini=/etc/php.ini
sed -i.bak 's/somepattern/changedpattern/' "$php_ini"

Note my use of lowercase variable names. Uppercase ones should be reserved for use by the shell.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!