6

I have the following code snippet

for num
do
   echo $num
done

But I don't understand why it works! How come does Bash know to loop through my params $1, $2, $3, $... using my personal taste of "num" variable???

13

It's a feature of bash:

for num; do ...

is a shorthand for

for num in "$@"; do ...

The documentation is at http://www.gnu.org/software/bash/manual/bashref.html#Looping-Constructs

1
  • 2
    For the record, this is not bash-specific feature. It was present all the way back to original Bourne shell. ( Learned so from old System V book ) – Sergiy Kolodyazhnyy Jan 6 '17 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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