Finding Commands
An environment variable is a named value the shell keeps for every command. PATH is one such value, and it lists the folders that hold commands.
Where a command lives
Ls and cat both live in /usr/bin.
$ cd ~/practice
$ which ls
$ which catWhat kind of command
Ls is a program, and cd is a shell builtin.
$ cd ~/practice
$ type ls
$ type cdRead a value
The dollar sign reads the value of PATH. The path on your machine differs.
$ cd ~/practice
$ echo $PATHList one variable
Env lists variables. Grep picks out one line.
$ cd ~/practice
$ env | grep '^HOME='Example
$ cd ~/practice
$ which grep
$ echo $HOMEKey points
- Use which to find where a command lives.
- Use type to tell what kind of command a name is.
- A shell builtin is a command built into the shell itself.
- Echo prints the value of a variable.
- A dollar sign before a name reads its value.
- Env lists the variables and their values.
- The PATH on your machine differs from the one shown here.
- An environment variable is a name with a value, kept by the shell.
In this example, which finds grep at /usr/bin/grep. Then echo reads the value of HOME, and the value is /home/hatch. The dollar sign turns the name into its value.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does which ls print?