Bash capturing output of awk into array

Add additional parentheses, like this:

myarr=($(ps -u kdride | awk '{ print $1 }'))

# Now access elements of an array (change "1" to whatever you want)
echo ${myarr[1]}

# Or loop through every element in the array
for i in "${myarr[@]}"
do
   :
  echo $i
done

See also bash — Arrays.


Use Bash's builtin mapfile (or its synonym readarray)

mapfile -t -s 1 myarr < <(ps -u myusername | awk '{print $1}')

At least in GNU/Linux you can format output of ps, so no need for awk and -s 1

mapfile -t myarr < <(ps -u myusername -o pid=)

Tags:

Linux

Arrays

Bash