which depends on TH & shelly which depends on a lot of things and also directory.
Tangent: it is useful to know that which is a clone of command -v which seems like always was in POSIX:
"The early Unix shells until the late 70s had ... only the traditional looking up of executables in $PATH" (which is command -v), Bourne shell (sh) had it also, seems like always. The which is a script from "3BSD (1980)" that introduced this functionality into csh, it stuck because of popularity of csh and the name being mnemonic.
command -v works even in fish, though, not being documented in it (... oh, now is).
Long story short - command -v is a shell builtin.
That is why declaring the requirement of pkgs.which in the Nix derivation overlay triggered my attention. If there is sh present - the which executable is not needed. Or for programming languages - if in OS where there is at least POSIX.1 (1988) support (which now again includes Microsoft Windows, not mentioning others, so we can claim POSIX.1 supported "always"), the replacement of basic which use is 1 call to system function.
POSIX API has a function named system:
NAME
system - issue a command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
The system() function shall behave as if a child process were created using fork(), and the child process invoked the sh utility using execl() as follows:
execl(<shell path>, "sh", "-c", command, (char *)0);
So the system function and sh features are isomorphic and central to the POSIX and most OSes, and so their use in programming languages is trivial.
system allows to run in the env any commands, so even the command -v which can run without the need of locating the shell (system(command)).
Knowing these 2 things allows understanding what can be done with direct use of POSIX calls, what sh can do - in Haskell also "should be" one-several POSIX calls away.
which
depends on TH &shelly
which depends on a lot of things and alsodirectory
.Tangent: it is useful to know that
which
is a clone ofcommand -v
which seems like always was in POSIX: "The early Unix shells until the late 70s had ... only the traditional looking up of executables in $PATH" (which iscommand -v
), Bourne shell (sh
) had it also, seems like always. Thewhich
is a script from "3BSD (1980)" that introduced this functionality intocsh
, it stuck because of popularity ofcsh
and the name being mnemonic.command -v
works even infish
, though, not being documented in it (... oh, now is). Long story short -command -v
is a shell builtin.That is why declaring the requirement of
pkgs.which
in the Nix derivation overlay triggered my attention. If there issh
present - thewhich
executable is not needed. Or for programming languages - if in OS where there is at leastPOSIX.1
(1988) support (which now again includes Microsoft Windows, not mentioning others, so we can claimPOSIX.1
supported "always"), the replacement of basicwhich
use is 1 call to system function.POSIX API has a function named
system
:So the
system
function andsh
features are isomorphic and central to the POSIX and most OSes, and so their use in programming languages is trivial.system
allows to run in the env any commands, so even thecommand -v which
can run without the need of locating theshell
(system(command)
).Knowing these 2 things allows understanding what can be done with direct use of POSIX calls, what
sh
can do - in Haskell also "should be" one-several POSIX calls away.