Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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)

share|improve this question

4 Answers 4

up vote 6 down vote accepted

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

For example:

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

removes from .bash_profile every line containing the string myapp.

share|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.

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

Try the vim-way:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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