1

So I have this path as my present working directory:

/home/oleg/.nvm/versions/node/v7.2.1/lib/node_modules

Using a bash script, I want to determine if this directory is within my user home directory and if so, also check to see if

/home/$USER/<dir> ==? /home/$USER/.nvm

In other words, looking to check if the first directory in the path below my user home, is named ".nvm".

What is the best way to do this? (Ideally this could also run on MacOS).

All I really need to do is check if

//pseudobash:

assert(String($PWD).startsWith("$HOME/.nvm"))

but doing that in bash, not sure

  • 1
    dirs=$(find . -maxdepth 1 -type d); [[ "${dirs[@]}" =~ ".nvm" ]] && printf "%s\n" "Success"... – jasonwryan Dec 21 '16 at 6:24
1

Do:

[[ $PWD =~ ^$HOME/\.nvm/ ]] && echo "In .nvm"
  • [[ $PWD =~ ^$HOME/\.nvm/ ]] checks if the $PWD starts with your home directory, followed by .nvm/ using Regex (=~) in the [[ keyword

  • If so (&&) then echo "In .nvm" is run

If you don not have =~ support (e.g. if you are using an old bash), use globbing instead:

[[ $PWD = $HOME/.nvm/* ]] && echo "In .nvm"

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.