I have an array containing strings to exclude with grep
from the output of another program. I need to add an -e
before each element. For instance:
exclude=("$0" /usr/sbin/crond) needs-restarting | grep -Fwiv "${exclude[@]}"
Now I know in this case I could prepend --regexp=
(or just -e
) to each element like so: exclude=( "${exclude[@]/#/--regexp=}" )
But in the general case, how would I go about it? I came up with this but maybe there's a simpler way.
i=0 for elem in "${exclude[@]}"; do exclude[i]='-e' exclude[i+1]="$elem" ((i+=2)) done declare -p exclude
"${exclude[@]/#/foo}"
that you're using? That seems pretty general to me, what am I missing?-e
its own element. When doing that substitution, it would be added to the existing element, which wouldn't work correctly when using it as an argument as they would need to be separate tokens.