I have posted this golfing tip for Bash before:

In Pure Bash (or any other pure shellscript), use . for looping.

It's a tip that recomments using a file whose the program has. I have ever posted these answers that uses the tip:

But I have thought that these situations can be thought:

  • The program's file is placed on / (root directory), which prevents from using cd / to end the loop.
  • The program's file is read-only, which prevents from using >x to end the loop.
  • The program is invoked from different directory, such as invoked with bash /path/to/x, bash ../../x
  • The program's directory has several files such as bash, zsh, yes, rm -Rf / that can have arbitrary data such as different shell script so invoking things such as for x in *; do ...; done, *, eval *, . * results in several behaviours.
  • The shell script is invoked with the flag such as -u and -e.

What assumptions must be given for such programs?

  • \$\begingroup\$ It's worth noting that in bash/shell, you can work around this using $0, which contains the filename of the program. \$\endgroup\$
    – pxeger
    Nov 13, 2021 at 8:27
  • 1
    \$\begingroup\$ But pathname and filename can have blank characters, so eventually "$0" is the safest as far as I know. \$\endgroup\$
    – user100411
    Nov 13, 2021 at 9:15
  • 2
    \$\begingroup\$ Ah, of course - too much Zsh (which doesn't have word splitting) \$\endgroup\$
    – pxeger
    Nov 13, 2021 at 14:21


You must log in to answer this question.