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)


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.

| improve this answer | |

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.

| improve this answer | |
  • 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 'SO- stop being evil' May 29 '13 at 21:10
sed -i '/^export MYAPP_HOME=\/opt\/myapp$/d' ~/.bash_profile
| improve this answer | |

Try the vim-way:

ex -s +"g/MYAPP_HOME/d" -cwq file.txt
| improve this answer | |

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.