2

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?

Ian
  • 23

1 Answers1

1

Because cd is not a "utility", it's a shell "bultin", handled by env's parent shell.

Read man $SHELL.

waltinator
  • 4,865
  • I figured that might be the problem, but since 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:23
  • cd 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, and env'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:41
  • It's a "utility" in the sense that POSIX uses that word (heck, the description starts by saying "the cd utility shall..."). Just that on some systems it's not available as a standalone program, one with a program file of its own, but only as a builtin utility of the shell. And if env 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 while env runs, the parent shell doesn't really do anything. – ilkkachu Apr 14 '21 at 11:41