Skip to main content
2 of 3
edited title
Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k

Bash "for" loop without a "in foo bar..." part

I was recently looking at some code that confused me because it works and I didn't expect it to. The code reduces to this example

#!/bin/bash
for var;
do
  echo "$var"
done

When run with command line arguments is prints them

$ ./test a b c
a
b
c

It is this, that is (to me) unexpected. Why does this not result in an error because var is undefined ? Is using this considered 'good practice' ?

user270650
  • 361
  • 3
  • 3