Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'which' vs 'command -v' in Bash [duplicate]

Tags:

bash

I've read in some bash FAQ a while ago (that I don't remember), that which should be avoided and command -v preferred.

Why is that so? What are the advantages, disadvantages of either one?

like image 911
adrelanos Avatar asked May 05 '16 17:05

adrelanos


People also ask

How do I duplicate a file in bash?

Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ).

What is &2 in bash script?

and >&2 means send the output to STDERR, So it will print the message as an error on the console. You can understand more about shell redirecting from those references: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Redirections.

What is ls in bash?

First Command: ls The ls command lists the files in your current directory (ls is short for "list").

What is the uniq command used for?

The uniq command can count and print the number of repeated lines. Just like duplicate lines, we can filter unique lines (non-duplicate lines) as well and can also ignore case sensitivity. We can skip fields and characters before comparing duplicate lines and also consider characters for filtering lines.


1 Answers

Well...

command is likely built in to your shell, and with the -v option will tell you how your shell will invoke the command specified as its option.

which is an external binary, located at /usr/bin/which which steps through the $PATH environment variable and checks for the existence of a file.

A reason to select the former over the latter is that it avoids a dependency on something that is outside your shell.

The two commands do different things, and you should select the one that more closely matches your needs. For example, if command is built in to your shell, command -v command will indicate this with its output (through the non-existence of path), but which command will try to point to a file on your path, regardless of how command would be interpreted by your shell.

like image 200
ghoti Avatar answered Oct 16 '22 22:10

ghoti