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:
- Implement a Truth-Machine
- 1, 2, Fizz, 4, Buzz
- Swap every two elements in a list
- Tips for golfing in Bash
- Print 0 to 100 without 1-9 characters
- Uncomment a COBOL program!
But I have thought that these situations can be thought:
- The program's file is placed on
/
(root directory), which prevents from usingcd /
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 asfor 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?
$0
, which contains the filename of the program. \$\endgroup\$"$0"
is the safest as far as I know. \$\endgroup\$