1

I want to store multiple grep parameters in a bash variable, so I can define the parameters in the configuration section at the top of my file, and use it in multiple locations.

How do I need to define the variable and write the grep command?

Details

My first attempt

# CONFIG: grep parameters to further filter ...                                                                  
GREP_PARAM="-E .*"

# ...

grep "^Stop " $1 | grep $GREP_PARAM | sed "..." >>$TFILE

results in

grep: ..: Is a directory

Using .\* or .\\* instead causes grep to not match anything, instead of everything.

Using grep "$GREP_PARAM" instead only works if GREP_PARAM contains a single parameter, but not otherwise; e.g. if it contains -v .*SAT.* or -v .\*SAT.\* or -v .\\*SAT.\\*, I get

grep: invalid option --

1 Answer 1

4

This is exactly what arrays were introduced to handle.

grep_options=(-E '.*')

grep "^Stop " "$1" | grep "${grep_options[@]}" | sed "..." >> "$TFILE"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.