Search code examples
linuxbashshellredhatarchlinux

How to add new input to the configuration file using bash script


I want to add new input to the configuration file from terminal using bash script. This is what I tried:

echo Hello, please add the new text here
read varname
sed -i "s/\<my-images=>/& $varname/" /home/myconfig
echo Image $varname has been added to the configuration. Thanks!!

/home/myconfig has

id=1
max-mb=1000
my-images=customimage

And required output is

id=1
max-mb=1000
my-images=mynewtext customimage

So mynewtext should be added after my-images= Anyway to do this?


Solution

  • The problem is with the regex match you're passing to sed. Try:

    sed -i "s/my-images=/&$varname /" /home/myconfig
    

    instead.