1

I'm trying to get a simple if statement working in both Zsh and Bash but I can't find something to work with both.

# this works with Zsh
if [[ "$TERM_PROGRAM" =~ iTerm\|Apple_Terminal ]]; then echo apples; fi

# this works with Bash
if [[ "$TERM_PROGRAM" =~ iTerm|Apple_Terminal ]]; then echo apples; fi

Cheers.

2 Answers 2

3

It looks like this is one case where putting it in a variable first helps. This works for me in both zsh and bash:

pattern='^iTerm|Apple_Terminal$'
if [[ "$TERM_PROGRAM" =~ $pattern ]]; then echo apples; fi

(Note: I added anchors to the pattern, to avoid matching things like "not_iTerm".)

0
2

I couldn't find a way to escape the regex that worked on both Bash and Zsh.

Here's a workaround I am using:

echo "$TERM_PROGRAM" | egrep -q 'iTerm|Apple_Terminal'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .