Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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:

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".)

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

 
discard

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.