Skip to main content
Align comments
Source Link
Tom Hale
  • 33.4k
  • 42
  • 165
  • 258

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

if [ -n "$PATH" ]; then old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:"$x":*) ;;  # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} unset old_PATH x fi 

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

if [ -n "$PATH" ]; then old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:"$x":*) ;; # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} unset old_PATH x fi 

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

if [ -n "$PATH" ]; then old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:"$x":*) ;;  # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} unset old_PATH x fi 
you don't want to set $PATH to an empty string if it was previously unset. No need for `set -f` or touching IFS here. Missing quotes.
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

set -f # Turnif off[ globbing,-n to"$PATH" allow]; unprotectedthen  variable substitutions IFS=: old_PATH=$PATH:; PATH=  while [ -n "$old_PATH" ]; do   x=${old_PATH%%:*} # the first remaining entry   case $PATH: in   *:${x}"$x":*) :;; # already there   *) PATH=$PATH:$x;; # not there yet   esac   old_PATH=${old_PATH#*:}  done  PATH=${PATH#:} set +f; unset IFS old_PATH x fi 

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

set -f # Turn off globbing, to allow unprotected variable substitutions IFS=: old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:${x}:*) :;; # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} set +f; unset IFS old_PATH x 

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

if [ -n "$PATH" ]; then  old_PATH=$PATH:; PATH=  while [ -n "$old_PATH" ]; do   x=${old_PATH%%:*} # the first remaining entry   case $PATH: in   *:"$x":*) ;; # already there   *) PATH=$PATH:$x;; # not there yet   esac   old_PATH=${old_PATH#*:}  done  PATH=${PATH#:} unset old_PATH x fi 
fixed set -f (thanks Burhan Ali)
Source Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

set -gf # Turn off globbing, to allow unprotected variable substitutions IFS=: old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:${x}:*) :;; # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} set +g;+f; unset IFS old_PATH x 

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

set -g # Turn off globbing, to allow unprotected variable substitutions IFS=: old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:${x}:*) :;; # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} set +g; unset IFS old_PATH x 

If you don't already have duplicates in the PATH and you only want to add directories if they are not already there, you can do it easily with the shell alone.

for x in /path/to/add …; do case ":$PATH:" in *":$x:"*) :;; # already there *) PATH="$x:$PATH";; esac done 

And here's a shell snippet that removes duplicates from $PATH. It goes through the entries one by one, and copies those that haven't been seen yet.

set -f # Turn off globbing, to allow unprotected variable substitutions IFS=: old_PATH=$PATH:; PATH= while [ -n "$old_PATH" ]; do x=${old_PATH%%:*} # the first remaining entry case $PATH: in *:${x}:*) :;; # already there *) PATH=$PATH:$x;; # not there yet esac old_PATH=${old_PATH#*:} done PATH=${PATH#:} set +f; unset IFS old_PATH x 
Source Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
Loading