which
The which command is used to locate the executable file associated with a given command by searching through the directories listed in the PATH environment variable.
To check where gcc compiler is located:
which gcc
Handling Non-existent Commands
Your team lead mentions that TechCorp used to use a custom build tool called techbuild. Let’s check if it’s still installed:
which techbuild
This is because which doesn’t return anything if it can’t find the command in your PATH. Don’t be alarmed by the lack of output - it’s the command’s way of telling you that techbuild isn’t found in any of the directories in your PATH.
To make this behavior more explicit, we can use a simple shell script. We’ll introduce two new concepts here:
A shell script is a text file that stores commands and runs them in order.
The cat <<'EOF' ... EOF pattern writes multiple lines into a file without making you manage several nested quotes.
Let’s create a script to check for techbuild:
cat > ~/check_techbuild.sh <<'EOF'
#!/bin/bash
if which techbuild >/dev/null 2>&1; then
echo "techbuild is installed"
else
echo "techbuild is not installed"
fi
EOF
This command creates a file named ~/check_techbuild.sh and fills it with a small Bash script.
Here is what the important lines mean:
#!/bin/bash tells Linux to run the script with Bash.
which techbuild checks whether the command can be found in your PATH.
>/dev/null 2>&1 hides normal output and error output so the script can print its own clear result message.
Now give the file execute permission and run it:
chmod +x ~/check_techbuild.sh
The chmod +x command adds execute permission, which allows Linux to run the file as a command.
~/check_techbuild.sh
Discovering Multiple Installations
The -a option of which can help you discover all installations of a command:
which -a python
/usr/local/bin/python
/usr/bin/python
/bin/python
This output indicates that there are multiple Python installations on the system. The -a option tells which to print all matching executables in PATH, not just the first one.
Understanding this is crucial because it shows you all the possible Python executables that could be run, depending on your PATH configuration. The first one listed is the one that will be executed when you type python in the terminal.
Creating a Custom Command
our team lead suggests creating a simple custom command to demonstrate how which interacts with the PATH. Let’s create a script called hello in your home directory:
cat > ~/hello <<'EOF'
#!/bin/bash
echo "Hello from TechCorp!"
EOF
chmod +x ~/hello
This creates a new file named hello in your home directory and makes it executable. The script prints Hello from TechCorp! when it runs.
Now, try to run it:
hello
You’ll get a “command not found” error because your home directory isn’t in the PATH. The PATH is a list of directories where the shell looks for executable files.
Let’s add your home directory to the PATH temporarily:
export PATH="$PATH:$HOME"
This command adds your home directory to the end of the PATH for the current terminal session. The export command makes the updated PATH available to commands you run afterward.
Now you can run hello, and which hello will find it:
hello
which hello
Summary
In this lab, you’ve learned how to use the which command to locate executable files in your system’s PATH. You’ve discovered how to:
Find the location of installed programs
Handle non-existent commands
Discover multiple installations of the same command
Understand PATH priority
Create and locate custom commands
You’ve also practiced writing simple shell scripts, making files executable with chmod +x, and adjusting PATH to control which command runs.
These skills will be invaluable as you continue your journey as a developer at TechCorp and beyond.