This is one way:
args=("key1" "key2" "key3") keys=${args[@]/%/\\|} # result: key1\| key2\| key3\| keys=${keys// } # result: key1\|key2\|key3\| grep "${keys}" file_name
Edit:
Based on the proposal of Pavel Shved :
( IFS="|"; keys="${args[*]}"; keys="${keys//|/\\|}"; grep "${keys}" file_name )
First version as single line:
keys=${args[@]/%/\\|}; keys=${keys// }; grep "${keys}" file_name
Edit2:
Even better than the version using IFS :
printf -v keys "%s\\|" "${args[@]}"; grep "${keys}" file_name
Dennis williamson
source share