I'm studying the env
command and trying to understand how it works. Here's the command synopsis:
env [-iv] [-P altpath] [-S string] [-u name] [name=value ...] [utility [argument ...]]
I decided to play around with it and tried:
env cd /home/username
I get: env: ‘cd’: No such file or directory
The result is the same with either env cd ~
or env cd
.
So why do I get an error when using cd as env's utility argument?
env pwd
works fine, I thought it must be something else. But I just found out that when utility is a built-in, the results are actually undefined: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/env.html. Thanks for the reply. – Ian Apr 13 '21 at 23:23cd
is a utility and is not a special built-in utility. A corresponding executable is required by POSIX and its absence is an issue of POSIX non-conformance of the system, andenv
's specific handling of special-builtins does not come into play. Indeed,env cd
is specifically required to work. – Michael Homer Apr 14 '21 at 04:41env
is also implemented as a standalone program, it can't access the builtins of the shell. I'm not sure what to say about that last part: "handled by env's parent shell.", since whileenv
runs, the parent shell doesn't really do anything. – ilkkachu Apr 14 '21 at 11:41