Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7924081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:32:31+00:00 2026-06-03T17:32:31+00:00

I need to manage a configuration file in Linux using a shell script, that

  • 0

I need to manage a configuration file in Linux using a shell script, that will read and write settings to and from the cfg file. I have following function to write a new value of a key in existing config file used in my script.

set_setting() {
    sed -i "s/$1=[^ ]*/$1=$2/" $3
}

Usage:

set_setting keyname NewValue /home/Config.cfg

Thus, this would replace the value of keyname in Config.cfg to NewValue.

I also have a function get_setting which uses source command to read the config file. It accepts similarly two parameters, keyname and the source config file, and returns the value of the supplied key.

Now the problem is that, I have config file as follows.

name=Abc
surname=Xyz

And when I call the set_setting function as set_setting name Def /home/Config.cfg, ideally it should change value of name from Abc to Def, but along with changing value of key name, it also changes the value of surname from Xyz to Def resulting into following in config file.

name=Def
surname=Def

I suspect this is due to having the term “name” common in both keys, as when I changed the key surname to surnames, it worked as expected and only value of name was changed. Also note that, the value for the key can be anything except the space.

I’m new to shell scripting and recently discovered sed command.

I’ve also refered to SO questions this and this. While both of them were quite helpful, I’m stuck with above mentioned situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:32:37+00:00Added an answer on June 3, 2026 at 5:32 pm

    You should anchor your match to the beginning of the line:

     sed -i "s/^$1=[^ ]*/$1=$2/" $3
    

    Note the ^ in front of $1. This regex operator anchors the expression to the beginning of the line, effectively requiring the name being everything from the first character to the = sign. There’s also an end-of-line anchor called $ (which you do not need here).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a configuration file from a data file that looks as
I need to pass configuration settings to an AddIn created using the Managed AddIn
I have about 5-6 Server Manager programs that write their own configuration file out
I need to create a configuration file in ~/.config/myapp.cfg So I am doing this
I'm building a app that need manage money datatype. I'm new on Obj-c, so
We need to find a sensible way to manage our in-house .NET libraries that
i have a web.config file that looks like this: <?xml version=1.0?> <configuration> <system.web> <compilation
I need to manage hierarchy data storing in my database. But I have a
We have a need to manage a large number (approx 20+) languages for our
I am in desperate need of help, I need to manage an application dependency

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.