Shell PATH
PATH is the list of folders the shell searches for commands. When you type a command name, the shell looks through these folders in order.
Add a folder to PATH
export puts mybin at the front of PATH. command -v shows where greet is found.
mkdir -p mybin
printf '#!/bin/bash\necho "Hello from mybin"\n' > mybin/greet
chmod +x mybin/greet
export PATH="mybin:$PATH"
command -v greetRun your own command
Once the folder is in PATH, you run greet by name, like any other command.
mkdir -p mybin
printf '#!/bin/bash\necho "Hello from mybin"\n' > mybin/greet
chmod +x mybin/greet
export PATH="mybin:$PATH"
greetThe usual home folder
$HOME/bin is the usual place for your own commands. Add it to PATH the same way.
mkdir -p home/bin
printf '#!/bin/bash\necho "Hello from bin"\n' > home/bin/hello
chmod +x home/bin/hello
export PATH="$HOME/bin:$PATH"
helloExample
mkdir -p mybin
printf '#!/bin/bash\necho "Hello from mybin"\n' > mybin/greet
chmod +x mybin/greet
export PATH="mybin:$PATH"
command -v greet
greetKey points
- PATH: the list of folders the shell searches for commands.
- The shell checks the folders in PATH in order and runs the first match.
- Use export PATH="$HOME/bin:$PATH" to add a folder to the front of PATH.
- Use command -v to show where the shell finds a command.
In this example, a small script called greet is made in mybin. The folder joins PATH, command -v finds greet there, and greet runs by name.
Note
A change to PATH lasts only in the current shell, unless you save it in a startup file.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What happens when you type a command name?