Path#
This page explains how Linux determines what to execute when you run any command in it. There is a special environment variable, PATH
, that specifies where to search for programs to be executed when a command is invoked from the shell. Each path is separated by a colon (:
). When any command is typed, the system checks all paths listed in PATH
for a corresponding file. When it finds the appropriate file, it executes it.
Extending#
PATH
can be extended using the command export PATH=$PATH:<new section of path>
, which modifies the PATH
environment variable by appending a new section to it.
The following cell creates a new script that prints Hello, World!
to the standard output and makes it executable.
[ -d /tmp/path_extending/ ] && rm -r /tmp/path_extending
mkdir /tmp/path_extending
cd /tmp/path_extending
cat << EOF > show_me
echo "Hello world"
EOF
chmod +x show_me
You can easily run it by specifying a direct path.
./show_me
Hello world
But after adding the appropriate path to the PATH
variable, you can run the command from any path you like. Following cells show the thing.
export PATH=$PATH:/tmp/path_extending
cd ~/
show_me
Hello world
Find path (which
)#
Sometimes it’s useful to retrieve the path of a command available in the shell. This can be done using the which <command>
syntax. Use which -a <command>
to display all paths to a given command.
The following cell applies the which
command to the ls
command.
which ls
/usr/bin/ls
To guarantee that the result is correct, let’s copy the ls
command and name it hello
. As a result, hello
should behave just like ls
.
cp /usr/bin/ls /usr/bin/hello
The following cell invokes hello /tmp
, and the result looks just like the files available in the /tmp
folder.
hello /tmp
devcontainers-6aa171e4-89a9-4aaf-bb84-1cd72716f30d1737908561740
devcontainers-e932ddd3-9bb4-406f-943b-62f41235e1a91737827817547
devcontainers-fbe44d37-c6c2-43ad-bc83-76e66fde933d1737907502847
vscode-ipc-1a675766-8108-4b19-a9c6-b2ddff78e404.sock
vscode-ipc-791e9e66-9310-45f3-a55e-f91811b29434.sock
vscode-ipc-9415dfa0-4e3c-48bd-8640-fe0f3fbed7f4.sock
vscode-ipc-fb102fc3-61d7-42ae-9e80-e12b7a136bf5.sock
vscode-remote-containers-0da91551-794b-4873-832f-b827f3d0b9b7.js
vscode-remote-containers-83e67190-1f4d-4195-a09f-62dbe1f51e94.js
vscode-remote-containers-d6916203-e4d1-40f2-9ef2-1135c4ff3e04.js
vscode-remote-containers-ipc-0da91551-794b-4873-832f-b827f3d0b9b7.sock
vscode-remote-containers-ipc-83e67190-1f4d-4195-a09f-62dbe1f51e94.sock
vscode-remote-containers-ipc-d6916203-e4d1-40f2-9ef2-1135c4ff3e04.sock
vscode-remote-containers-server-0da91551-794b-4873-832f-b827f3d0b9b7.js
vscode-remote-containers-server-83e67190-1f4d-4195-a09f-62dbe1f51e94.js
vscode-remote-containers-server-d6916203-e4d1-40f2-9ef2-1135c4ff3e04.js
vscode-ssh-auth-0da91551-794b-4873-832f-b827f3d0b9b7.sock
vscode-ssh-auth-83e67190-1f4d-4195-a09f-62dbe1f51e94.sock
vscode-ssh-auth-d6916203-e4d1-40f2-9ef2-1135c4ff3e04.sock
You can even find the path to the which
command itself.
which which
/usr/bin/which
The next cell creates the file /tmp/second_ls/ls
and adds it to PATH
to demonstrate how which
behaves in this case.
mkdir /tmp/second_ls
touch /tmp/second_ls/ls
export PATH=$PATH:/tmp/second_ls/
By simply invoking which ls
, it shows only one path to the original ls
, as it appears first in PATH
.
which ls
/usr/bin/ls
But which -a ls
shows both pathes.
which -a ls
/usr/bin/ls
/bin/ls