Skip to main content
Fix typo that changed the meaning
Source Link
Toby Speight
  • 89.6k
  • 14
  • 105
  • 329

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument be passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front, being a constant string needs, need not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument be passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front being a constant string needs not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument be passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front, being a constant string, need not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Fix grammar/typos
Source Link
user34073
user34073

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument isbe passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front being a constand strings needconstant string needs not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument is passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front being a constand strings need not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument be passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front being a constant string needs not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

added 607 characters in body
Source Link

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument is passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front being a constand strings need not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

Here is one way to make it more portable and robust:

add2path() {
  if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
    if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
      if [ "$2" = front ]; then # Use a standard shell test
        PATH="$1:$PATH"
      else
        PATH="$PATH:$1"
      fi
      export PATH
    fi
  fi
}

I have added a test to quit the function with an error should no argument is passed.

PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.

Using [[ is not specified by POSIX so it is better to stick with [ to do tests.

$2 should then be quoted to avoid clashes with [ operands.

front being a constand strings need not be quoted.

Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.

Source Link
Loading