Detect if user's path has a specific directory in it

前端 未结 8 2251
天命终不由人
天命终不由人 2020-11-28 07:13

With /bin/bash, how would I detect if a user has a specific directory in their $PATH variable?

For example

if [ -p \"$HOME/bin\" ]; then         


        
8条回答
  •  北海茫月
    2020-11-28 08:02

    There is absolutely no need to use external utilities like grep for this. Here is what I have been using, which should be portable back to even legacy versions of the Bourne shell.

    case :$PATH: # notice colons around the value
      in *:$HOME/bin:*) ;; # do nothing, it's there
         *) echo "$HOME/bin not in $PATH" >&2;;
    esac
    

提交回复
热议问题