9

I try to remove string from .bash_profile. String is added when my shell script run:

My string at bash_profile as follows:

# for Myapllication
export MYAPP_HOME=/opt/myapp

I want to remove the strings from .bash_profile when myapp is removed via rpm.

How to remove any string from a file via shell script? (or possible alternative method)

10

You can remove a string from a text file with sed (other tools exist).

For example:

sed -i -e '/myapp/d' .bash_profile

removes from .bash_profile every line containing the string myapp.

10

A file like ~/.bash_profile lives in a home directory of a user. Such a file is completely under control of the user. Global acting commands like rpm are not supposed to change such files.

  • You usually have a base configuration file, which is delivered by the rpm package.

  • You then have a global configuration file which can be used by root to overwrite some preferences specific to the given system.

  • Then you have personal configuration files in your home directory which you can use to override the global setting with your personal preferences.

A command like rpm should only change the first one and never change the latter.

  • Can you suggest a URL to examine for this topic? – Gyhot May 29 '13 at 15:20
  • 1
    @Gyhot unix.stackexchange.com/questions/77518/… is a good one. If your target distribution has a /etc/profile.d directory, drop a file containing that export line there. Otherwise, you may edit /etc/profile, but be very careful not to wreck customizations by the system administrator. – Gilles May 29 '13 at 21:10
3
sed -i '/^export MYAPP_HOME=\/opt\/myapp$/d' ~/.bash_profile
0

Try the vim-way:

ex -s +"g/MYAPP_HOME/d" -cwq file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.