I have an array with PIDs in my bash script. I would like to join these with \| as separator as to be able to use grep to search for anything that matches any of the PIDs in the array. I am basically trying to change the IFS as shown below but the problem I encounter is that instead of the desired output:
GREP_ARG='29126\|27435'
I get
GREP_ARG='29126\27435'.
This is the code I am using
function join {
local IFS="$1"; shift; echo "$*";
}
GREP_ARG=$(join '\|' "${PID_ARRAY[@]}")
grep -A1 $GREP_ARG file
I have tried to change the input in varius ways but nothing works. Is the IFS approach to this not possible?
You can do
GREP_ARG=$(printf "\|%s" "${PID_ARRAY[@]}")
GREP_ARG=${GREP_ARG:2}