Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exactly what does env do in Bash?

Tags:

bash

I get this behavior when using Bash (under Cygwin):

$ printf '\u00d5'
\u00d5
$ env printf '\u00d5' # This results in the behavior I want
Õ

It does not matter if I use UTF-8 or ISO-8859-1 encoding in the terminal.

My questions are: Exactly what does env do? Why do I need it in this specific case?

like image 374
Deleted Avatar asked Oct 02 '12 13:10

Deleted


People also ask

What does env do in Bash?

The env command allows you to display your current environment or run a specified command in a changed environment. If no flags or parameters are specified, the env command displays your current environment, showing one Name=Value pair per line.

What is the purpose of env?

env is a shell command for Unix and Unix-like operating systems. It is used to either print a list of environment variables or run another utility in an altered environment without having to modify the currently existing environment.

Is env a shell variable?

They can be one of two types, environmental variables or shell variables. Environmental variables are variables that are defined for the current shell and are inherited by any child shells or processes. Environmental variables are used to pass information into processes that are spawned from the shell.

What is env script?

Environment Script Plugin allows you to have a script run after SCM checkout, before the build. If the script fails (exit code isn't zero), the build is marked as failed. Any output on standard out is parsed as environment variables that are applied to the build.


1 Answers

env is not in bash, it's a standalone executable, it is used to set or clear environment variable before running program. In your particular case, it's running the binary printf instead of the shell builtin. You can achieve the same result by using the absolute path:

/usr/bin/printf '\u00d5'

The least intrusive method is perhaps the following: redefine the printf function and have Bash handle the rest. Source a file that contains the following:

function printf()
{
  $(which printf) "$@"
}

or as a one-liner function printf() { $(which printf) "$@"; }. Of course you could replace the $(which printf) for a /usr/bin/printf ...

Then simply use printf as you're used to. Your script stays the same and you could even introduce a condition to define the function only on certain Bash versions.

AFAIK you can also leave out the function, but I find it adds to readability.

[EDIT: the function keyword is a bash extension; printf () { ...; } is the POSIX syntax. If you do use the function keyword, the () after the function name are optional.]


Commonly, env is also used in the hash-bang lines of scripts that strive to be portable. The reason being that env is nearly always at /usr/bin/env, while bash isn't always at /bin/bash as many a hash-bang line implies. Example:

#!/usr/bin/env bash

also works for other programs/interpreters:

#!/usr/bin/env python
like image 64
Michael Krelin - hacker Avatar answered Sep 19 '22 07:09

Michael Krelin - hacker