I'm writing a config bash script to build some environment on my Ubuntu.

One thing that needs to be done in this bash script is to append a function into ~/.bashrc so that it could be executed automatically while logging.

However, for some reason, this script might be executed several times. As a result, the same function is appended into ~/.bashrc many times.

Is there a way to solve the problem?

1 Answer 1


Instead of blindingly appending, check if it's there or not already.

something like grep -q theFunction .bashrc || echo " theFunction() { .... } " >> .bashrc

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.