Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


share|improve this question
up vote 3 down vote accepted

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

if [[ "$TERM_PROGRAM" =~ $pattern ]]; then echo apples; fi

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

share|improve this answer
Thanks, Gordon. – Odin Nov 6 '12 at 23:12

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'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.