Your syntax was almost correct:
IFS=' ' read -r -a pidarray <<< "$testpid" echo "${pidarray[0]}"
Note the curly braces needed to dereference an array.
More importantly, check that your shebang is #!/bin/bash
, or that you executed your script with bash yourscript
, not sh yourscript
. The specified error is consistent with a shell that does not recognize <<<
as a valid syntax that any remote modern bash will always have when calling bash; even if /bin/sh
points to the same executable, it tells bash to disable many of its extensions from the POSIX specification.
Now that you have said - if your goal is to assign each record to your own variable, you do not need (and should not use) read -a
at all!
Instead
IFS=' ' read -r first second third rest <<<"$testpid" printf '%s\n' "First PID is $first" "Second PID is $second"
source share