Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Digital Literacy

Maria De Bruyn
Maria De Bruyn
4,635 Points

"which" command line for console

I was wondering if anyone had a good explanation for what which does on the console? Or any good links that explain "which"?

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Maria De Bruyn , the syntax of this command is which command_name, which returns the command's Path. For example

which bash
=> /bin/bash

It tells you where the bash command in the current Shell environment is located at, in another word, the path of bash command's executable.

For some simple reference, you may check out this page http://www.computerhope.com/unix/uwhich.htm

which is one of the simplest command in UNIX, there's not a whole lot to talk about it; but the reason why which command might be useful is because, all the UNIX commands are just small utilities installed somewhere on your system, oftentimes there're multiple copies/versions of the same command installed on different directories, which command_name is a great way to find out which copy of the command you're currently using in the Terminal.

It's also worth knowing that, in the case where multiple copies of the same command are presented in your system, when the Terminal is fired up, it'd only load the first copy of the command it's able to locate at the shell's Lookup PATH environment, and ignore the rest that may comes after it.

For example, OS X comes with ruby command installed on the system path usr/bin/ruby, sth like that; but no Ruby developer wants to use it, since it's always outdated and there's no easy way to update it; so most developers choose to install a separate copy of Ruby to the Home directory using Ruby Version Manager or rbenv. Now there's 2 copies of Ruby executable on the system, which ruby gives valuable feedback on which ruby command the shell is currently using.

Maria De Bruyn
Maria De Bruyn
4,635 Points

Thanks, this is really helpful.

Which shows you the absolute path in the file structure of a command. For example if I typed "which apache2" it might return something like "/var/sbin/apache2".