Using the "whence" Command (with examples)

Using the "whence" Command (with examples)

  • Osx
  • November 5, 2023

The “whence” command is a zsh builtin that allows you to investigate how a given command would be interpreted. It provides various options to display information about the command, such as its type, location, and occurrences on the command path. This article will guide you through different use cases of the “whence” command with code examples.

Use Case 1: Interpret command with expansion

To interpret a command and determine its type (e.g., an alias or a function), you can use the “whence” command with the command as an argument. Here is an example:

whence "ls"

Motivation: This use case is useful when you want to check if a given command is defined as an alias and if it expands to another command. It helps you understand how a command is being interpreted by your shell.

Explanation: By executing the command “whence “ls””, the shell will interpret the “ls” command and display its expanded form if it is an alias.

Example output: If “ls” is defined as an alias to “ls -F –color=always”, the output of the command will be:

ls: aliased to ls -F --color=always

Use Case 2: Display type and location of command

The “whence” command can also display the type of a command and its location if it is defined as a function or a binary. This behavior is equivalent to the “type” and “command -V” builtins. Here is an example:

whence -v "grep"

Motivation: This use case can be helpful when you want to know if a command is defined as a shell function or if it points to a binary executable. It provides information about the source of the command and its location.

Explanation: By using the “-v” option with the “whence” command, it will display the type of the command (“function” or “binary”) and its location if it is defined as a function or a binary.

Example output: If “grep” is defined as a function in your shell, the output of the command will be:

grep is a shell function from /usr/local/bin/grep

If “grep” is a binary executable and its location is in the system’s PATH, the output will be:

grep is /usr/bin/grep

Use Case 3: Display content of shell function

The “whence” command can also display the content of a shell function instead of its location. This behavior is equivalent to the “which” builtin. Here is an example:

whence -c "ls"

Motivation: This use case is helpful when you want to view the content of a shell function. It allows you to understand what the function does and how it is implemented.

Explanation: By using the “-c” option with the “whence” command, it will display the content of the shell function instead of its location. This is applicable only if the command is defined as a function.

Example output: If “ls” is defined as a shell function in your shell, the output of the command will be:

ls () {
    command ls -F --color=always $argv;
}

Use Case 4: Display all occurrences on command path

The “whence” command can be used to display all occurrences of a command on the command path. This behavior is equivalent to the “where” builtin. Here is an example:

whence -ca "python"

Motivation: This use case is useful when you want to know all the locations where a command is found on the command path. It helps you identify if there are multiple versions or installations of a command.

Explanation: By using the “-ca” option with the “whence” command, it will search for all occurrences of the command on the command path and display their locations.

Example output: If “python” is found in multiple locations on the command path, the output of the command will be:

python is /usr/bin/python
python is /usr/local/bin/python

Use Case 5: Search only the PATH for command

The “whence” command can be used to search only the PATH for a command, ignoring builtins, aliases, or shell functions. This behavior is equivalent to the “where” command. Here is an example:

whence -p "nano"

Motivation: This use case is helpful when you want to search for an executable command in the system’s PATH and exclude any builtins, aliases, or shell functions with the same name.

Explanation: By using the “-p” option with the “whence” command, it will search only the PATH for the specified command.

Example output: If “nano” is found in the system’s PATH, the output of the command will be:

nano is /usr/bin/nano

Conclusion:

The “whence” command is a versatile tool for investigating how a given command would be interpreted in the shell. It provides various options to display information about the command, its type, location, and occurrences on the command path. By understanding these different use cases and their respective options, you can have better control and insights into the commands used in your shell environment.

Tags :

Related Posts

How to use the command clementine (with examples)

How to use the command clementine (with examples)

Clementine is a modern music player and library organizer that provides various commands to control music playback and manage playlists.

Read More
How to use the command "qm rescan" (with examples)

How to use the command "qm rescan" (with examples)

The “qm rescan” command is used to rescan all storages of a virtual machine in Proxmox VE and update the disk sizes.

Read More
How to use the command apt-key (with examples)

How to use the command apt-key (with examples)

The apt-key command is a key management utility for the APT Package Manager on Debian and Ubuntu.

Read More