11

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)

Gyhot
  • 267
  • 2
  • 4
  • 9

4 Answers4

14

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.

lgeorget
  • 13,656
  • 2
  • 41
  • 63
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.

michas
  • 21,190
  • 4
  • 63
  • 93
  • Can you suggest a URL to examine for this topic? – Gyhot May 29 '13 at 15:20
  • 1
    @Gyhot http://unix.stackexchange.com/questions/77518/how-to-remove-any-string-from-a-file-via-shell-scripts/77522#77522 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
3
sed -i '/^export MYAPP_HOME=\/opt\/myapp$/d' ~/.bash_profile
Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
0

Try the vim-way:

ex -s +"g/MYAPP_HOME/d" -cwq file.txt
kenorb
  • 20,250
  • 14
  • 140
  • 164