How to remove any string from a file via shell scripts?

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.


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.


sed -i '/^export MYAPP_HOME=\/opt\/myapp$/d' ~/.bash_profile