If a shell is asked to perform a probably useless (or partially useless) command known to terminate, such as cat hugeregularfile.txt > /dev/null
, can it skip that command's execution (or execute a cheaper equivalent, say, touch -a hugeregularfile.txt
)?
More generally, is the shell similar to C compilers in that it may perform any transformation on the source code, so long as the externally observable behaviour is as-if the abstract machine evaluated it?
EDIT
Nota Bene: My question as originally posed had a title that asked whether the shell is permitted to do these optimizations, not whether it should or even whether implementations that can do them exist. I'm interested in the theory more than the practice, although both are welcome.
cat
ting it makes a big difference. The shell can get to know that the file is a device, but it need not be reliable.wc
, for instance). But to the best of my knowledge POSIX doesn't take a position on shell optimization; Or does it?ksh
. Like they don't say separate process but subshell environment to allow fork-saving optimisations.