8.4.40 which command (obtains the paths of external commands)
Syntax
which[-a]command-name ...
Description
This command obtains the paths of external commands to be executed from the command search path set in the PATH environment variable. The command outputs the obtained command paths to the standard output.
Arguments
- -a
Specifies that all executable command paths are to be obtained from the command search path set in the PATH environment variable.
When the -a option is omitted, the command output only the first command path obtained.
- command-name
Specifies the name of an external command whose command path is to be obtained. You can specify multiple command names.
If the command path of a specified external command is not found, the which command outputs a message to that effect to the standard error output.
Command path search rules
The command searches for the command paths of external commands according to the rules described below.
- In Windows:
- Paths subject to external command search
The command searches the command search path set in the PATH environment variable for the external commands. If multiple command paths are set in the PATH environment variable, the command searches the command paths in order from the beginning. If the user executing the which command does not have permissions to read the external command storage directory, that directory is not subject to command path search.
- External commands whose command paths are to be output
If the user executing the which command has permissions to read the external command storage directory, the which command outputs the corresponding command paths. The which command does not check whether the user has permissions to execute the external command.
The which command can output the paths of external commands if they are executable files with the extension .com, .exe, .cmd, or .bat.
If a specified external command contains no extension, the which command adds the extensions defined in the PATHEXT environment variable in the order defined and then searches the external commands. The supported extensions are .com, .exe, .cmd, and .bat. For details, see 5.1.11 Specifying external commands.
- In UNIX:
- Paths subject to external command search
The command searches the command search path set in the PATH environment variable for the external commands. If multiple command paths are set in the PATH environment variable, the command searches the command paths in order from the beginning. If the user executing the which command does not have permissions to search the external command storage directory (including all directories in the path), that directory is not subject to command path search.
- External commands whose command paths are to be output
If the user executing the which command has permissions to execute a specified external command, the which command determines that that external command is executable and outputs its command path. If the user does not have permissions to execute a specified external command, that external command's path is not output.
When the command names specified in the argument contain paths
- In Windows:
If the user executing the which command has permissions to read the external command storage directory, the which command outputs the corresponding command paths. The which command does not check whether the user has permissions to execute the external command.
If the user executing the which command does not have permissions to read the external command storage directory, the which command outputs a message indicating that the external command's command path was not found.
The which command can output the paths of external commands if they are executable files with extension .com, .exe, .cmd, or .bat.
If the specified external commands contain no extension, the which command adds to the external command names the extensions defined in the PATHEXT environment variable in the order defined. The supported extensions are .com, .exe, .cmd, and .bat.
This command behaves as follows if the specified external command is a symbolic link.
-
Execution permission is determined to be evaluated if both extensions of the symbolic link and file at the link destination are .com, .exe, .cmd, or .bat.
-
If the specified external commands contain no extension, add extensions to the PATHEXT environment variable in the order defined. However, no extension is added to a file at the link destination. The supported extensions are .com, .exe, .cmd, and .bat.
-
- In UNIX:
If the user executing the which command has permissions to search the external command storage directory (including all directories in the path) and the external command execution permissions, the which command outputs the command names specified in the argument. If the user executing the which command does not have these permissions, the which command outputs a message indicating that the external command's command path was not found.
Return code
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 |
Error termination The command path of the external command was not found; or, if multiple external commands were searched, at least one external command's command path was not found. |
2 |
Error termination
|
Notes
If the PATH environment variable is undefined, the command terminates with an error.
In Windows, if the PATHEXT environment variable is undefined, the command terminates with an error.
If the following names are specified as command names in the argument, the which command treats them as external commands:
Aliases defined by the alias command
Reserved words, standard shell commands, extended shell commands, and functions
If a command name specified in the argument contains a path and the path name is subject to conversion by either of the environment setting parameters listed below, the which command outputs the path name obtained after conversion:
PATH_CONV parameter
COMMAND_CONV_ARG parameter
In Windows, only external commands that satisfy the command path search rules are subject to output of command paths.
When you execute an external command from the awk, find, or xargs command, the path search rules of the external command apply as follows:
Method of external command execution
Path search rules
-
system function of awk command
-
Format of the awk command: command-name | getline [variable-name]
-
Format of the awk command: print [expression[, ... ]] | command-name
The path search rules for command processor execution (such as the command prompt) apply
-
-exec primary of find command
-
-ok primary of find command
-
xargs command
The path search rules of the Windows API that executes the program apply
Note that the command path that is output when a command name specified in the above command is specified in the argument might differ from the path of the command that is executed.
-
Examples
Obtain the command path of command pgm01.exe:
C:\TEMP>%ADSH_OSCMD_DIR%\which pgm01.exe C:\Program Files\Hitachi\PP001\pgm01.exe
Obtain the command path of command pgm01. This example omits the extension of the command name:
C:\TEMP>%ADSH_OSCMD_DIR%\which pgm01 C:\Program Files\Hitachi\PP001\pgm01.exe
Specify the -a option to obtain all command paths of command pgm01.exe:
C:\TEMP>%ADSH_OSCMD_DIR%\which -a pgm01.exe C:\Program Files\Hitachi\PP001\pgm01.exe C:\Program Files\Hitachi\PP002\pgm01.exe C:\Program Files\Hitachi\PP003\pgm01.exe
Obtain the command path of command pgm02. In this example, the command search path does not contain pgm02:
C:\TEMP>%ADSH_OSCMD_DIR%\which pgm02 which: no pgm02 in (C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Hitachi\PP001 ;C:\Program Files\Hitachi\PP002;C:\Program Files\Hitachi\PP003)
Obtain the command paths of the commands pgm01,pgm02, pgm03, and pgm04. In this example, the command search path does not contain the commands pgm02 and pgm04:
C:\TEMP>%ADSH_OSCMD_DIR%\which pgm01 pgm02 pgm03 pgm04 C:\Program Files\Hitachi\PP001\pgm01.exe which: no pgm02 in (C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Hitachi\PP001 ;C:\Program Files\Hitachi\PP002;C:\Program Files\Hitachi\PP003) C:\Program Files\Hitachi\PP001\pgm03.exe which: no pgm04 in (C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Hitachi\PP001 ;C:\Program Files\Hitachi\PP002;C:\Program Files\Hitachi\PP003)
Execute the which command with a command name containing a path specified. In this example, the specified program name exists:
C:\TEMP>%ADSH_OSCMD_DIR%\which "C:\Program Files\Hitachi\PP001\pgm01" C:\Program Files\Hitachi\PP001\pgm01.exe
Execute the which command with a command name containing a path specified. In this example, the specified program name does not exist:
C:\TEMP>%ADSH_OSCMD_DIR%\which "C:\Program Files\Hitachi\PP001\pgm02" which: no pgm02 in (C:\Program Files\Hitachi\PP001)