Tutorial

How to Identify and Modify $PATH for Command Availability

Mister P
by  Mister P · on
Linux
Learn how to inspect $PATH, add a directory for the current shell session, and persist the change in ~/.bash_profile so it survives a new login shell. A core skill for RHCSA EX200 exam objective 101.

How to Identify and Modify $PATH for Command Availability

Note

Before running any commands: click the + button in the terminal tab bar to open a new terminal tab. The playground history tracking activates in new sessions only. Commands run in the original tab will not register for task verification.

This tutorial walks you through inspecting $PATH, adding a directory for the current shell session, and persisting that change so it survives a new login shell. On the RHCSA EX200 exam you may need to make a custom command findable without knowing its full path — this is how.

The playground has already created /opt/myapp/bin/mycommand for you to work with.


Step 1 — Inspect the Current $PATH

The shell resolves command names by searching each directory listed in $PATH from left to right, stopping at the first match. Knowing what is already there — and in what order — is the starting point before making any change.

Display the current search path:

echo $PATH

The output is a colon-separated string that is hard to read at a glance. Pipe it through tr to see one directory per line:

echo $PATH | tr ':' '\n'
Note

Order matters. A directory at position 1 is searched before position 8. If two directories contain a binary with the same name, the one found first wins.


Step 2 — Check Whether Your Directory Is Already in $PATH

Before modifying anything, confirm the directory is actually missing:

echo $PATH | tr ':' '\n' | grep '/opt/myapp/bin'

If the command returns no output, /opt/myapp/bin is not in $PATH. That is the expected result here — proceed to Step 3.


Step 3 — Add the Directory for the Current Session

You have two options depending on the priority you need.

Prepend (new directory searched first — takes priority over any existing command of the same name):

export PATH=/opt/myapp/bin:$PATH

Append (new directory searched last — existing system commands take priority):

export PATH=$PATH:/opt/myapp/bin

For this exercise use the prepend form so /opt/myapp/bin appears at the front of the list.

Important

This change applies to the current shell session and any child processes it spawns. It is lost when the terminal closes. Step 4 makes it permanent.

After running the export, confirm the directory is now present:

echo $PATH | tr ':' '\n'
The command is still not found after the export

Check for a typo in the path. Run echo $PATH | tr ':' '\n' and look for /opt/myapp/bin in the output. If the line is missing, re-run the export command exactly as shown. A common mistake is writing export PATH=/opt/myapp/bin (without :\$PATH), which discards the entire original PATH and leaves you with only that one directory.


Step 4 — Persist the Change in ~/.bash_profile

Session exports disappear when the terminal closes. To make the change survive a new login shell, add the export line to ~/.bash_profile:

echo 'export PATH=/opt/myapp/bin:$PATH' >> ~/.bash_profile
Important

Use single quotes around the export line. Single quotes prevent the shell from expanding $PATH right now — you want the literal string written to the file so the shell expands it at login time against the then-current PATH value. Double quotes would expand $PATH immediately, hard-coding your current session's PATH into the file.

Confirm the line was written correctly:

grep 'opt/myapp/bin' ~/.bash_profile
I accidentally added it to ~/.bashrc instead of ~/.bash_profile

On Rocky Linux 9, a graphical or non-login interactive shell reads ~/.bashrc, while a login shell reads ~/.bash_profile. For the RHCSA exam, the expected location for PATH persistence is ~/.bash_profile. You can add the line to both files to cover all shell types, but always ensure ~/.bash_profile has it. Check with: grep opt/myapp/bin ~/.bashrc ~/.bash_profile


Step 5 — Verify the Change Survives a New Login Shell

Force Bash to re-read the login profile without fully logging out:

bash --login -c 'echo $PATH | tr ":" "\n"'

Look for /opt/myapp/bin in the output. This simulates exactly what happens when a new login session starts.

The login shell output does not show /opt/myapp/bin

Run bash -n ~/.bash_profile to check for syntax errors in the file. If the command reports an error, open the file and fix the offending line. If there are no syntax errors but the directory is still missing, verify the exact content of the file with cat ~/.bash_profile and confirm the export line reads exactly: export PATH=/opt/myapp/bin:$PATH


Final Verification

Confirm the directory is present and in the expected position:

echo $PATH | tr ':' '\n' | grep -n '/opt/myapp/bin'

A low line number (e.g., 1) means the directory was prepended and will be searched first. A high line number means it was appended.

Confirm the command in the added directory is now found by the shell:

type mycommand

Expected output:

mycommand is /opt/myapp/bin/mycommand

Troubleshooting Reference

ProblemCauseSolution
Command still not found after adding directoryTypo in path or export not savedRun echo $PATH | tr ':' '\n' and confirm the exact path appears
Change lost after opening a new terminalExport in ~/.bashrc instead of ~/.bash_profile, or terminal opens a non-login shellVerify the export line is in ~/.bash_profile; for non-login interactive shells, also add it to ~/.bashrc
New login shell does not pick up the change~/.bash_profile not saved, or syntax error aborted loadingRun bash -n ~/.bash_profile to check for syntax errors
Prepended directory is still searched after /usr/bin$PATH was omitted from the export, discarding the old valueUse export PATH=/opt/myapp/bin:$PATH, not export PATH=/opt/myapp/bin
Permission denied running a command in the added directoryBinary is not executableRun chmod +x /opt/myapp/bin/mycommand

What You Did

  1. Inspected $PATH and read its directories one per line with tr
  2. Checked whether a target directory was already present before modifying anything
  3. Added a directory for the current session with export PATH=...
  4. Persisted the change in ~/.bash_profile using single-quoted echo redirection
  5. Verified the change loads in a new login shell with bash --login -c '...'

These five steps are exactly what the RHCSA exam expects when you are asked to make a custom binary available by name without specifying its full path.

Note

Exam tip: On the RHCSA, ~/.bash_profile is the correct file for login-shell PATH changes. If you are unsure whether the exam shell is a login shell or a non-login interactive shell, add the export line to both ~/.bash_profile and ~/.bashrc to be safe.

About the Author

Mister P

Mister P

Find this author online

More tutorials you might like

Learn by doing, not just by reading or watching

Sign up for a free account to start a VM playground right on this page, track your progress, and get notified about new learning materials.

Sign up for free