Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is it that hard to remember a bash for loop?

  for i in $(seq 1 10)
  do echo $i
  done
Then you can just replace new lines with ; if writing a one liner.

  for i in $(seq 1 10); do echo $i; done
I guess it isn't as simple as python

  for i in range(10): print(i)
but it's pretty close aside from the variable binding.


Bash has some subtleties around loops that can be frustrating, like the body of your loop _may_ actually be running in a subshell (common when iterating over a file), making it difficult to extract what you want from it.


i bet it's the `cat file | while read …` loop what you are referring to, not any loop. but correct me.


In zsh you can avoid the do if you have a single instruction:

    for i in {1..10}; echo $i
to me is as clear as the python version. Very compact and useful for one liners.


As someone dealing with new remote machines on a daily basis, I cannot fantasize about any other shell. Bash is pretty straightforward.


  for i in {1..10}


And look at all of those finger bending symbols!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: