Variables, PATH and .bashrc
You see the difference between shell and environment variables with export, learn how PATH finds commands and why its order matters, ask what a command really is with type and which, edit .bashrc and load it with source; at the end you catch a fake command hidden in a world-writable PATH directory, keep the evidence and remove it. The fake command differs in every lab.
Level 101 · 45 min
An administrator says: "something is odd on this server: some commands work normally, but it is as if someone is watching." Daniel suspects: "Look at PATH."
When you type a command, the shell searches the directories in the PATH variable from left to right and runs the first file it finds. Someone who puts a program with the same name into a directory early in the list can shadow the real command. This is called PATH hijacking. The figure shows how the search works.
First you learn the variables themselves: a variable defined in the shell belongs only to that shell; if it is exported, it passes on to child processes too.
What you will do in this lesson
- Define and use a variable
- export: pass it to child processes
- Read PATH
- What is this command? type and which
- .bashrc and source
- Permissions of the PATH directories
- Find shadowing commands
- What does the fake command do?
- Keep the evidence and clean up
- Short quiz
Skills you will earn
- Shell variables and quoting
- Environment variables (export, printenv)
- PATH and the command search order
- Asking what a command is (type, which -a)
- Editing and loading .bashrc (source)
- Finding a fake command hidden in PATH
- Writing a short, precise finding
← Exit status and chaining commands · Linux fundamentals · Aliases, command substitution and history →
Start for free