Search code examples
bashawkifs

bash IFS awk $2


How can I use IFS and awk $2 value on the same line?

ex.

array=(
  element:5001
  element:5002
  element:5003
  element:5004
)
IFS=':'
for i in "${array[@]}"
  do
  set -- $i

  part1=$1
  part2=$2

  cd $part1
  # this following line is where I am having the issue.
  # $2 equal to part2 from the string split, but the $2 to be
  # awk value for $2
  echo "$(ps aux | grep '[s]omething --port '$2'' | awk '{print $2}')"
  # use part2 later in code
done

Presently, I am only able to use $2 from IFS split.

Note: I have seen post about using the -v option with awk but I believe that is when you want to set a value to run against awk. I would like to print $2 generated from awk.


Solution

  • Thanks to @Barmar and @l0b0.

    I need to fix my quotes.

    ...
    echo $(ps aux | grep '[s]omething --port '$2 | awk '{print $2}')
    ...