简体   繁体   中英

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

You can use BASH regex for this:

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

OR using shell glob:

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM