Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

This question is a sequel of sorts to my earlier question. The users on this site kindly helped me determine how to write a bash for loop that iterates over string values. For example, suppose that a loop control variable fname iterates over the strings "a.txt" "b.txt" "c.txt". I would like to echo "yes!" when fname has the value "a.txt" or "c.txt", and echo "no!" otherwise. I have tried the following bash shell script:


for fname in "a.txt" "b.txt" "c.txt"
 echo $fname
 if [ "$fname" = "a.txt" ] | [ "$fname" = "c.txt" ]; then
 echo "yes!"
 echo "no!"

I obtain the output:







Why does the if statement apparently yield true when fname has the value "a.txt"? Have I used | incorrectly? Thanks for your time.

share|improve this question
In bash, 'or' operator is '||' (C style). – Marius Cotofana Sep 8 '12 at 20:47
You can also use -o within the same [ ]. – Thor Sep 8 '12 at 20:53
@Thor I'd prefer || and separate [ ] over -o for portability simply because [ is not guaranteed to support more than 4 arguments. Of course if the target language is bash, no one should be using [ anyways because bash's [[ is superior in many ways. – jw013 Sep 9 '12 at 0:46
@jw013 Thanks. Does this mean that I should be using if [[ "$fname" = "a.txt" ]] || [[ "$fname" = "c.txt" ]] rather than if [ "$fname" = "a.txt" ] || [ "$fname" = "c.txt" ]? – Andrew Sep 9 '12 at 17:14
@Andrew That is correct, if as you are declaring the shebang as bash, as you are already doing. One advantage of [[ is that it doesn't do word splitting (special case) so [[ $unquoted_var = string ]] is safe. – jw013 Sep 10 '12 at 2:06
up vote 41 down vote accepted

If you want to say OR use double pipe (||).

if [ "$fname" = "a.txt" ] || [ "$fname" = "c.txt" ]

The code in your question is simply piping the output of the left side to the right side, in the same way any ordinary pipe works.

share|improve this answer
Correct answer, but faulty explanation. || does an OR operation on the two commands to the left and right: if the left side exits non-zero, the right side is evaluated. It's not "piping the output... to the right side". – raehik Mar 18 at 10:55
The explanation is correct, @raehik. It describes the code in the question, not the code in the answer. – Wildcard May 18 at 6:17

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.