🎓 A1Learner
🔍
0 of 50 lessons completed

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 cat

What kind of command

Ls is a program, and cd is a shell builtin.

$ cd ~/practice
$ type ls
$ type cd

Read a value

The dollar sign reads the value of PATH. The path on your machine differs.

$ cd ~/practice
$ echo $PATH

List one variable

Env lists variables. Grep picks out one line.

$ cd ~/practice
$ env | grep '^HOME='

Example

$ cd ~/practice
$ which grep
$ echo $HOME

Key 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?