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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Are there any historical reasons for there being two commands instead
of one?
There was, just history manner.
- Bill Joy wrote the first version of
printenvcommand in 1979 for BSD. - UNIX System III introduced
envcommand in 1980. - GNU followed UNIX System’s
envin 1986. - BSD followed GNU/UNIX System’s
envin 1988. - MINIX followed BSD’s
printenvin 1988. - GNU followed MINX/BSD’s
printenvin 1989. - GNU shell programming utilities 1.0 included
printenvandenvin 1991. - GNU Shell Utilities merged into GNU coreutils in 2002, which is what you find in GNU/Linux nowadays.
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 before env 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
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980 Regents of the University of California.n
All rights reserved.n";
#endif not lint
#ifndef lint
static char sccsid[] = "@(#)printenv.c 5.1 (Berkeley) 5/31/85";
#endif not lint
/*
* printenv
*
* Bill Joy, UCB
* February, 1979
*/
extern char **environ;
main(argc, argv)
int argc;
char *argv[];
{
register char **ep;
int found = 0;
argc--, argv++;
if (environ)
for (ep = environ; *ep; ep++)
if (argc == 0 || prefix(argv[0], *ep)) {
register char *cp = *ep;
found++;
if (argc) {
while (*cp && *cp != '=')
cp++;
if (*cp == '=')
cp++;
}
printf("%sn", cp);
}
exit (!found);
}
prefix(cp, dp)
char *cp, *dp;
{
while (*cp && *dp && *cp == *dp)
cp++, dp++;
if (*cp == 0)
return (*dp == '=');
return (0);
}
[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/
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="037b6a626c61626a437b6a626c61626a">[email protected]</a> pdp11v3]$ sudo grep -rni printenv . //no such printenv exist.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c04151d131e1d153c04151d131e1d15">[email protected]</a> pdp11v3]$ sudo find . -iname '*env*'
./sys3/usr/src/lib/libF77/getenv_.c
./sys3/usr/src/lib/libc/vax/gen/getenv.c
./sys3/usr/src/lib/libc/pdp11/gen/getenv.c
./sys3/usr/src/man/man3/getenv.3c
./sys3/usr/src/man/docs/c_env
./sys3/usr/src/man/docs/mm_man/s03envir
./sys3/usr/src/man/man7/environ.7
./sys3/usr/src/man/man1/env.1
./sys3/usr/src/cmd/env.c
./sys3/bin/env
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f8e9e1efe2e1e9c0f8e9e1efe2e1e9">[email protected]</a> pdp11v3]$ man ./sys3/usr/src/man/man1/env.1 | cat //but got env already
ENV(1) General Commands Manual ENV(1)
NAME
env - set environment for command execution
SYNOPSIS
env [-] [ name=value ] ... [ command args ]
DESCRIPTION
Env obtains the current environment, modifies it according to its arguments, then executes the command with the modified environment. Arguments of the form
name=value are merged into the inherited environment before the command is executed. The - flag causes the inherited environment to be ignored completely,
so that the command is executed with exactly the environment specified by the arguments.
If no command is specified, the resulting environment is printed, one name-value pair per line.
SEE ALSO
sh(1), exec(2), profile(5), environ(7).
ENV(1)
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a32232b25282b230a32232b25282b23">[email protected]</a> pdp11v3]$
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="38405159575a595178405159575a5951">[email protected]</a> pdp11v3]$ cat ./sys3/usr/src/cmd/env.c //diff with http://minnie.tuhs.org/cgi-bin/utree.pl?file=pdp11v/usr/src/cmd/env.c version 1.4, you will know this file is slightly older, so we can concluded that this file is "env.c version < 1.4"
/*
* env [ - ] [ name=value ]... [command arg...]
* set environment, then execute command (or print environment)
* - says start fresh, otherwise merge with inherited environment
*/
#include <stdio.h>
#define NENV 100
char *newenv[NENV];
char *nullp = NULL;
extern char **environ;
extern errno;
extern char *sys_errlist[];
char *nvmatch(), *strchr();
main(argc, argv, envp)
register char **argv, **envp;
{
argc--;
argv++;
if (argc && strcmp(*argv, "-") == 0) {
envp = &nullp;
argc--;
argv++;
}
for (; *envp != NULL; envp++)
if (strchr(*envp, '=') != NULL)
addname(*envp);
while (*argv != NULL && strchr(*argv, '=') != NULL)
addname(*argv++);
if (*argv == NULL)
print();
else {
environ = newenv;
execvp(*argv, argv);
fprintf(stderr, "%s: %sn", sys_errlist[errno], *argv);
exit(1);
}
}
addname(arg)
register char *arg;
{
register char **p;
for (p = newenv; *p != NULL && p < &newenv[NENV-1]; p++)
if (nvmatch(arg, *p) != NULL) {
*p = arg;
return;
}
if (p >= &newenv[NENV-1]) {
fprintf(stderr, "too many values in environmentn");
print();
exit(1);
}
*p = arg;
return;
}
print()
{
register char **p = newenv;
while (*p != NULL)
printf("%sn", *p++);
}
/*
* s1 is either name, or name=value
* s2 is name=value
* if names match, return value of s2, else NULL
*/
static char *
nvmatch(s1, s2)
register char *s1, *s2;
{
while (*s1 == *s2++)
if (*s1++ == '=')
return(s2);
if (*s1 == '' && *(s2-1) == '=')
return(s2);
return(NULL);
}
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceb6a7afa1acafa78eb6a7afa1acafa7">[email protected]</a> pdp11v3]$
[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
- Tanenbaum wrote a clone of UNIX, called MINIX (MINi-unIX), for the IBM PC. It was targeted at students and others who wanted to learn how an operating system worked.
[1988]
BSD 1st env.c //http://minnie.tuhs.org/cgi-bin/utree.pl?file=2.11BSD/src/usr.sbin/cron/env.c
/* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved
[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
The programs in this package are:
basename date dirname env expr groups id logname pathchk printenv
printf sleep tee tty whoami yes nice nohup stty uname
[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
/* printenv -- print all or part of environment Copyright (C) 1989, 1991 Free Software Foundation. ...
[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
--- NEW FILE: printenv.c --- /* * Copyright (c) 1993 by David I. Bell
[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 with printenv:
-
print environment variables, but
printenvcan do the same -
Disable shell builtin but can achieve with
enablecmd 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 pythonheader, but still not portable ifenvnot in /usr/bin -
env -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.shto 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.shand runenv -i bash /tmp/test.shagain. If something failed, undo it. Repeat the step to narrow down. [5] Finally I figure outeogrequires$DISPLAYto run incrontab, and absent of$DBUS_SESSION_BUS_ADDRESSwill 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 ofenvorprintenv.
e.g:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="740c1634101a0c16">[email protected]</a>:~$ sudo env | grep PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="750d1735111b0d17">[email protected]</a>:~$ sudo printenv | grep PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e19983a1858f9983">[email protected]</a>:~$ sudo printenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e766c4e6a60766c">[email protected]</a>:~$ sudo env PATH env: ‘PATH’: No such file or directory <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4dcc6e4c0cadcc6">[email protected]</a>:~$
Method 2
Having a different point of view (from FreeBSD), you have:
From man env:
The env utility executes another utility after modifying the environment as specified on the command line. Each name=value option specifies the setting of an environment variable, name, with a value of value. All such environment variables are set before the utility is executed. ... If no utility is specified, env prints out the names and values of the variables in the environment, with one name/value pair per line.
From man printenv:
The printenv utility prints out the names and values of the variables in the environment, with one name/value pair per line. If name is speci- fied, only its value is printed.
So these commands might have the same effect without argument, but printenv sole purpose is to display the current environment key/values while env goal it to set some environment before calling another binary/script/whatever.
Is it more clear this way ?
To known more:
man 1 env(FreeBSD)man 1 printenv(FreeBSD)
Method 3
env is POSIX 7, printenv is not (GNU Coreutils in Ubuntu 15.10).
Method 4
From man-pages:
env – run a program in a modified environment
…
printenv – print all or part of environment
Should be pretty explanatory.
Method 5
Talking strictly about functionalities, env is a binary with a huge set of features, one of them being printing environment variables, whereas printenv just prints environment variables.
Summarizing, if you are used to work with env, you will go with env for printing them (because that’s what you are used to) and if you are not, you will typically remember printenv faster.
There are practically no differences when talking about printenv vs env for just printing environmental variables. I just checked and env is slightly heavier (about 5 extra KB), and their performance (in time) seems to be exactly the same.
Hope this clears it out! 🙂
Method 6
If you really want to know how different output is of the two binaries regardless of the history and legacy of the them, you can run a few utilities to gauge this difference. On debian I ran a few things that will be different depending on any custom environmental vars:
env |wc -l printenv |wc -l
Both my output has 41 lines
env > env.txt printenv > printenv.txt diff env.txt printenv.txt
Output:
41c41 < _=/usr/bin/env --- > _=/usr/bin/printenv
So you see there is one line different between the two and that line is number 41 which I guess stipulates the binary used in the command. Without additional arguments, these report virtual identical info for me.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0