0

I have a function defined in my script:

export_path()
{
  array=($(echo $PATH | awk -F":" '{print $0}'))
  if ! [ $(echo ${array[*]} | grep "/usr/bin" > /dev/null 2>&1) ]; then export PATH=$PATH:/usr/bin;fi
  if ! [ $(echo ${array[*]} | grep "/usr/sbin" > /dev/null 2>&1) ]; then export PATH=$PATH:/usr/sbin;fi
  if ! [ $(echo ${array[*]} | grep "/bin" > /dev/null 2>&1) ]; then export PATH=$PATH:/bin;fi
  if ! [ $(echo ${array[*]} | grep "/sbin" > /dev/null 2>&1) ]; then export PATH=$PATH:/sbin;fi
 }

array=($(echo $PATH | awk -F":" '{print $0}')) works well for bash but not for sh.

I need to find a way to make the array definition work across all shells.

dig_123
  • 2,240
  • 6
  • 35
  • 59
  • 1
    Have a look at the 3rd answer from [this question](https://stackoverflow.com/questions/1878882/arrays-in-unix-shell) – Aserre Jun 12 '18 at 09:59
  • @Aserre Thanks for the reference. Got my answer – dig_123 Jun 12 '18 at 13:02
  • 1
    /bin/sh on recent versions of Debian (and Ubuntu) doesn't support arrays at all. But see [this question](https://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there) for other ways to accomplish the same thing. – Gordon Davisson Jun 12 '18 at 20:12

0 Answers0