Shell – what is `env ` doing

commandcommand lineenvshell

What is the command env ls -al doing?

I had a Linux test and there was question: "How to run command directly, but not its alias?"
I knew that there exists solution like prefixing command with some special symbol, but I forgot it. Now I know that it is \. (read from this post).

But I also remember that somewhere I read that to get rid of alias we can prefix a command with env. I did it and it seems works, but my answer was qualified as wrong.
I read info and man on env, but didn't understood too much.

What is env doing and exactly in env <command> without any arguments for env itself?

Best Answer

This command

env name=value name2=value2 program and args

runs the command program and args with an environment formed by extending the current environment with the environment variables and values designated by name=value and name2=value2. If you do not include any arguments like name=value, then the current environment is passed along unmodified.

The key thing that happens with respect to aliases is that env is an external command, so it has no “knowledge” of aliases: aliases are a shell construct that are not part of the normal process model and have no impact on programs that are directly run by non-shell programs (like env). env simply passes the program and arguments to an exec call (like execvp, which will search the PATH for program).

Basically, using env like this is a (mostly) shell-independent way of avoiding aliases, shell functions, shell builtin commands, and any other bits of shell functionality that might replace or override command-position arguments (i.e. program names)—unless, of course, env is an alias, or shell function! If you are worried about env being an alias, you could spell out the full path (e.g. /usr/bin/env, though it may vary).

Related Question