What is the difference between the two commands env
and printenv
? They both show the environment variables, and the output is exactly the same aside from _
.
Are there any historical reasons for there being two commands instead of one?
environment-variables
What is the difference between the two commands env
and printenv
? They both show the environment variables, and the output is exactly the same aside from _
.
Are there any historical reasons for there being two commands instead of one?
Best Answer
There was, just history manner.
printenv
command in 1979 for BSD.env
command in 1980.env
in 1986.env
in 1988.printenv
in 1988.printenv
in 1989.printenv
andenv
in 1991.Note that the "followed" doesn't means the source code was the same, probably they were rewritten to avoid license lawsuits.
So, the reason why both commands existed is because Bill Joy wrote
printenv
even beforeenv
existed. After 10 years of merging/compatibility and GNU come across it, you are now seeing both similar commands on the same page.This history indicated as follows: (I tried to minimize the answer and only provided 2 essential source code snippets here. The rest you can click the attached links to see more)
[fall of 1975]
Also arriving in the fall of 1975 were two unnoticed graduate students, Bill Joy and Chuck Haley; they both took an immediate interest in the new system. Initially, they began working on a Pascal system that Thompson had hacked together while hanging around the 11/70 machine room.
[1977]
Joy started compiling the first Berkeley Software Distribution (1BSD), which was released on March 9, 1978. //rf: https://en.wikipedia.org/wiki/Berkeley_Software_Distribution
[February, 1979]
1979(see "Bill Joy, UCB February, 1979") /1980(see "copyright[] =") , printenv.c //rf: http://minnie.tuhs.org/cgi-bin/utree.pl?file=2.11BSD/src/ucb/printenv.c
[1979]
Hard to determine released in 2BSD OR 3BSD //rf: https://en.wikipedia.org/wiki/Berkeley_Software_Distribution
3BSD The printenv command appeared in 3.0 BSD. //rf: http://www.freebsd.org/cgi/man.cgi?query=printenv&sektion=1#end 3.0 BSD introduced at 1979 //rf: http://gunkies.org/wiki/3_BSD
2BSD The printenv command first appeared in 2BSD //rf: http://man.openbsd.org/printenv.1
[June, 1980]
UNIX Release 3.0 OR "UNIX System III" //rf: ftp://pdp11.org.ru/pub/unix-archive/PDP-11/Distributions/usdl/SysIII/
[1985]
BSD first printenv manual //rf: http://minnie.tuhs.org/cgi-bin/utree.pl?file=2.11BSD/src/man/man1/printenv.1
I couldn't find the manual related to env, but the closest is getenv and environ //http://minnie.tuhs.org/cgi-bin/utree.pl?file=2.11BSD/src/man
[1986]
First version of GNU
env
//rf: ftp://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/i386/1.0-RELEASE/ports/shellutils/src/env.c[1987]
MINIX 1st released //rf: https://en.wikipedia.org/wiki/Andrew_S._Tanenbaum
[1988]
BSD 1st env.c //http://minnie.tuhs.org/cgi-bin/utree.pl?file=2.11BSD/src/usr.sbin/cron/env.c
[October 4, 1988]
MINIX version 1.3 //rf: https://groups.google.com/forum/#!topic/comp.os.minix/cQ8kaiq1hgI
... 32932 190 /minix/commands/printenv.c //printenv.c already exist
//rf: http://www.informatica.co.cr/linux/research/1990/0202.htm
[1989]
The first version of GNU
printenv
, refer to [August 12, 1993].[July 16, 1991]
"Shellutils" - GNU shell programming utilities 1.0 released //rf: https://groups.google.com/forum/#!topic/gnu.announce/xpTRtuFpNQc
[August 12, 1993]
printenv.c //rf: ftp://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/i386/1.0-RELEASE/ports/shellutils/src/printenv.c
, GNU Shell Utilities 1.8 //rf: ftp://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/i386/1.0-RELEASE/ports/shellutils/VERSION
[1993]
printenv.c which found on DSLinux source code in 2006 //rf: (Google) cache:mailman.dslinux.in-berlin.de/pipermail/dslinux-commit-dslinux.in-berlin.de/2006-August/000578.html
[November 1993]
The first version of FreeBSD was released. //rf: https://en.wikipedia.org/wiki/FreeBSD
[september 1, 2002]
http://git.savannah.gnu.org/cgit/coreutils.git/tree/README-package-renamed-to-coreutils
The GNU fileutils, textutils, and sh-utils(see "Shellutils" at July 16, 1991 above) packages have been merged into one, called the GNU coreutils.
Overall,
env
use cases compare withprintenv
:print environment variables, but
printenv
can do the sameDisable shell builtin but can achieve with
enable
cmd too.set variable but pointless due to some shells already can do it without
env
, e.g.$ HOME=/dev HOME=/tmp USER=root /bin/bash -c "cd ~; pwd"
/tmp
#!/usr/bin/env python
header, but still not portable ifenv
not in /usr/binenv -i
, disable all env. I find it useful to figure out the critical environment variables for certain program, to make it run fromcrontab
. e.g. [1] In interactive mode, rundeclare -p > /tmp/d.sh
to stores attributes variables. [2] In/tmp/test.sh
, write:. /tmp/d.sh; eog /home/xiaobai/Pictures/1.jpg
[3] Now runenv -i bash /tmp/test.sh
[4] If it success to display image, remove half of variables in/tmp/d.sh
and runenv -i bash /tmp/test.sh
again. If something failed, undo it. Repeat the step to narrow down. [5] Finally I figure outeog
requires$DISPLAY
to run incrontab
, and absent of$DBUS_SESSION_BUS_ADDRESS
will slow down the display of image.target_PATH="$PATH:$(sudo printenv PATH)";
is useful to directly use the root path without having to further parse the output ofenv
orprintenv
.e.g: