Comment

Comment by 0xbadcafebee

The author can't remember how to do a for-loop in Bash, so he invented something that is Python, but also isn't Python, because straight Python doesn't work in the context of Ken Thompson's original command shell from 1971?

....just so the author doesn't have to look at StackOverflow, here is his Python example in the video:

  for i in range(5):
      if i:
          print(i)
Here it is in POSIX Shell/Bourne Shell/Bash/whatever:

  for i in 1 2 3 4 5
  do
      echo $i
  done
The fancy version:

  for i in `seq 1 5` ; do
      if [ $i -gt 0 ] ; then
          echo $i
      fi
  done
Fancier:

  for i in $(seq 1 5) ; do
      [ $i -gt 0 ] && echo $i
  done
Mucho Fancy (Bash-only):

  while read -r NUM ; do
      [ "$NUM" -gt 0 ] && echo "$NUM"
  done < <(seq 1 5)

Uber Fancy: Internal Seq w/Arrays & References (Bash v4+ only):

  _internal_seq () {
      declare -n aref="$1"
      local counter="$2" end="$3"
      while [ "$counter" -le "$end" ] ; do
          aref+=("$counter")
          counter="$((counter+1))"
      done
  }
  declare -a numbers=()
  _internal_seq numbers 1 5
  for i in "${numbers[@]}" ; do
      echo "$i"
  done
The version I actually use as a one-liner:

  for i in `seq 1 5` ; do echo $i ; done

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton