zizther
zizther

Reputation: 854

Checking if PWD contains directory name

I want to find out if the PWD contains a certain directory name in it, it should be able to test it being anywhere in the output.

For example I have structure paths like public/bower_components/name/ and also have paths which are just public.

I want to test because the contents of the folder name move into the public folder and the bower_components folder is removed.

Thanks

Upvotes: 30

Views: 22968

Answers (2)

kojiro
kojiro

Reputation: 77167

You can use case:

case "$PWD" in
    */somedir/*) …;;
    *) ;; # default case
esac

You can use [[:

if [[ "$PWD" = */somedir/* ]]; then …

You can use regex:

if [[ "$PWD" =~ somedir ]]; then …

and there are more ways, to boot!

Upvotes: 10

anubhava
anubhava

Reputation: 785886

You can use BASH regex for this:

[[ "$PWD" =~ somedir ]] && echo "PWD has somedir"

OR using shell glob:

[[ "$PWD" == *somedir* ]] && echo "PWD has somedir"

Upvotes: 45

Related Questions