I am running a software where I want to check if miniconda is already installed. Therefore I've checked whether 'miniconda' or 'anaconda' strings in .bashrc file using the grep command. However, it also finds either of above strings which are commented out in the .bashrc file, which I do not want to have. How do I fix this? Relevant part of my bashscript looks as follows.

#Finding if miniconda or anaconda string is in bashrc  
if grep -qF -e miniconda -e anaconda "$HOME"/.bashrc ;then    
  echo "miniconda is found in .bashrc"

I've tested this by adding following lines in the .bashrc file.


Terminal Output

jen@scs400:/scratch$ source bash_script.sh     
miniconda is found in .bashrc
  • 2
    grep -E '(ana|mini)conda' .bashrc. – Videonauth Apr 29 at 16:54
  • @Videonauth: Thanks it works! But could you please explain why this works? – Jenny Apr 29 at 17:03
  • 1
    @Videonauth I don't see how that excludes commented lines - doesn't it need some kind of anchor like ^[^#]*(ana|mini)conda? – steeldriver Apr 29 at 17:09
  • @steeldriver it doesn't this was just a wild shot in the dark, feel freee to write an exhausting answer if you like. – Videonauth Apr 29 at 17:12
  • Your check for "miniconda is already installed" is incorrect/incomplete. Some users want to switch in and out of miniconda use, and keep the code to do this elsewhere. Some users source, or ., other files from within ~/.bashrc, – waltinator Apr 29 at 20:00

Try this:

if grep -qwE '^[^#]*(ana|mini)conda' "$HOME"/.bashrc ;then
    echo "miniconda/anaconda is found in .bashrc"
  • Note this won't work reliably, because # can appear as a non-comment character as well.. For example: search_string='#[0-9]+.* anaconda' . Or if [ "$1" -eq 3 ]; then echo "Arg #1 triggered change of target (value was 3)"; target="#$anaconda"; fi. Or even source '~/#scripts/.anaconda' (or for extremophiles, a shebang I guess: #!/bin/anaconda, or some compiler preprocessor directive marked with a '#' ?!). The OP will have to consider whether such statements are likely to be an issue, though. If it's an issue, your best bet is to detect/filter out static strings using a few chained sed – Stilez Apr 29 at 21:46

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.