1

I tried using sed to convert PATH paths as an input to find:

User.Name@Machine-Name ~
$ echo $PATH | sed -e 's/:/\n/g' \
  | sed -n -e 's=.*="&"=' -e '2,5p'

"/bin" "/usr/local/bin" "/usr/bin" "/c/Program Files/Eclipse Adoptium/jdk-8.0.392.8-hotspot/bin"

The paths are quoted to protect spaces. Unfortunately, they aren't recognized properly when submitted to find:

User.Name@Machine-Name ~
$ find \
   $(echo $PATH | sed -e 's/:/\n/g' | sed -n -e 's=.*="&"=' -e '2,5p') \
   -iname '*libc*'

find: ‘"/bin"’: No such file or directory find: ‘"/usr/local/bin"’: No such file or directory find: ‘"/usr/bin"’: No such file or directory find: ‘"/c/Program’: No such file or directory find: ‘Files/Eclipse’: No such file or directory find: ‘Adoptium/jdk-8.0.392.8-hotspot/bin"’: No such file or directory

In fact, the spaces aren't protected and are interpretted as argument delimiters.

On the other hand, if I simply type in a quoted path, it works fine:

User.Name@Machine-Name ~
$ find "/bin" -iname '*libc*' # Finds nothing, but no syntax error
User.Name@Machine-Name ~

How can I understand this and how can I efficiently convert PATH paths into arguments (say, for find)?

terdon
  • 242,166

1 Answers1

3

If you don't need to preserve empty elements in the PATH, then you could try

IFS=: read -ra patharray <<<"$PATH"

and then (noting that bash arrays are zero indexed)

find "${patharray[@]:1:4}" -iname '*libc*'
steeldriver
  • 81,074