Home > Computer Tutorials > Computer Knowledge > Which command in Linux

Which command in Linux

WBOY
Release: 2024-03-21 09:50:11
forward
843 people have browsed it

Which command in Linux

Title: Linux Which Command

Excerpt: A practical tutorial with examples on using the "which" command on Linux and how to find various executables and scripts from the PATH variable.

Permalink: linux-which-command

Category:Linux commands

In this guide, we will learn about the “Which” command in Linux.

prerequisites:

To perform the steps demonstrated in this guide, you will need the following components:

  • Correctly configured Linux system. For testing purposes, it is recommended to use a Linux VM
  • Have a basic understanding of the command line interface

which command

Modern Linux systems come with multiple tools built-in for various tasks such as system management, automation, system monitoring, and remote computing. The "which" command is one of them.

The main use of the "which" command is to locate the location of executable files (and scripts). Please see the example below:

$Which one is

here:

  • We have to ask "which" tells us the location of "ls"
  • The "which" command searches for the "ls" command in PATH. Learn more about the PATH environment variable on Linux.
  • When a match is found, the location of the file will be printed on the console (STDOUT).

Basic usage

To find the binary executable of a command/tool, use the "Which" command as follows:

$,

The "which" command also accepts multiple parameters. Check out the following examples:

$which ls man chmod python3

here:

  • We ask "which" to locate the executable files of "ls", "man", "chmod" and "python3"
  • The output prints the location of these binaries, one line per entry.

Find multiple executable files

In Linux systems, there can be multiple copies of the same tool path location. For example, /usr/bin, /usr/sbin, /bin, and /sbin have overlapping executables:

$ls—l/usr/bin

$ls-L/usr/sbin

$ls-L/bin

$ls—l/sbin

Although there are multiple copies, the shell only runs a specific copy of the executable (usually located under /usr/bin) whenever a command is run. By default, the "Which" command reports this location of the "default" executable.

However, we can instruct "which" to report the locations of all matching copies of an executable.

$which—Close

$which—Close chmod bash

Exit code

After running the query, the "which" command will leave an exit code. The value of the code indicates whether the operation was successful.

This is a list of all exit passwords

  • 0: The parameters are valid and executable.
  • 1: One or more parameters cannot be found or are not executable.
  • 2: The specified option is invalid.

In Bash, after running any command, the exit code is stored in a variable. To view the value, use the following command:

$which

$ECHO$?

$Which asdfg

$ECHO$?

If you plan to incorporate the "which" command into a shell script, it is also beneficial to know the exit code. If you're a beginner, check out this beginner's guide to Bash scripting.

Other documents

Most Linux tools come with detailed documentation outlining all available parameters. For more in-depth documentation, check out the man page:

$man,

path environment variable

Whenever any query is run, the "which" command looks for executable files in the directory specified in the PATH environment variable. In this section, we'll take a quick look at how to use PATH.

To view the contents of this variable, run the "ECHO" command as follows:

$ECHO$PATH

$ tr ‘:’ ‘\n’

The above is the detailed content of Which command in Linux. For more information, please follow other related articles on the PHP Chinese website!

source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template