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

前端 未结 8 2250
天命终不由人
天命终不由人 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:00

    This is a brute force approach but it works in all cases except when a path entry contains a colon. And no programs other than the shell are used.

    previous_IFS=$IFS
    dir_in_path='no'
    export IFS=":"
    for p in $PATH
    do
      [ "$p" = "/path/to/check" ] && dir_in_path='yes'
    done
    
    [ "$dir_in_path" = "no" ] && export PATH="$PATH:/path/to/check"
    export IFS=$previous_IFS
    

提交回复
热议问题