Learn › Linux Foundations › Chaining
$ - a hands-on Linux lab on a real virtual machine.
Meet $, the variable that holds your shell's own process ID. Learn what a PID is, prove $ is a live number that varies per session but never moves within one, and pick up the classic unique temp file trick, all in the practice terminal.
Here is something nobody has told you yet: the shell you are typing into is itself a running program. Like every running program on Linux, it carries a unique ID number. The system uses that number to tell it apart from everything else. The number is sitting one command away, and you have never seen it.
This lesson is about $, the tiny variable that holds that number. By the end you will know what the number means, why it is different for every person reading this, and the classic trick engineers use it for.
The black boxes below are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. The real machine comes at the end of the module, in the chaining mission lab, where you use $ for real.
Every program running on a Linux system gets a process ID, or PID for short: a plain number, unique while that program runs, like a ticket number at a deli counter. Your shell, the program reading your commands right now, has one too.
The variable $ always holds your current shell's PID. Type echo $ on a real machine and you get a live number, something like 2016007. Your number will be different, and you get a fresh one every time you open a new shell. That is the whole point: $ is a live ID, not a fixed value.
$ goes all the way back to the original Bourne shell from the 1970s. Scripters leaned on it for one job above all: naming temporary files like report-$.tmp. No two shells share a PID, so two shells running the same script could never overwrite each other's file.
Since your PID differs from everyone else's, this lesson cannot show you "the right number." But one thing is true on every machine: whatever $ holds, it is always a run of digits. So the drill proves the shape instead of the value.
The line below borrows two helpers, one you know and one you will meet properly later. The | is a pipe: it feeds the output of echo $ into the next command (its full lesson comes in the redirection module). That next command, grep, is a text-search tool with a full lesson coming later. Here it only answers one yes/no question: is this text all digits? Then && (remember the and-operator lesson) prints the message only if grep said yes.
echo $ | grep -Eq '^[0-9]+#39; && echo "yes: it is all digits"
Before you run it, decide: your PID is different from everyone else's, so will this check still pass on your machine?
prompt: student@linuxcamp:~$ answer: echo $ | grep -Eq '^[0-9]+
#39; && echo "yes: it is all digits" output: yes: it is all digits hint: echo dollar-dollar, a pipe, grep for digits, then && the message: echo $ | grep -Eq '^[0-9]+#39; && echo "yes: it is all digits"It passed, and here is the part worth staring at: it prints yes: it is all digits on every machine, no matter what the actual PID is. The number varies; the shape never does. That is why this one line is safe to practice on while echo $ alone is not: there is no single "correct" number to check you against. This digit-check line is the workhorse of the lesson, and you just ran it.
You now own the workhorse form. Time to look inside it. The -q in grep -Eq means quiet: grep prints nothing at all and reports its yes/no purely through its exit code. Remember the rule from the exit codes lesson: 0 means success, and $? reads the code of the last command.
So drop the && message and read the code yourself instead:
echo $ | grep -Eq '^[0-9]+#39;; echo $?
Before you run it, decide: grep will find digits, so what single number should echo $? print?
prompt: student@linuxcamp:~$ answer: echo $ | grep -Eq '^[0-9]+
#39;; echo $? output: 0 hint: Same pipe into grep, then a semicolon and echo $?: echo $ | grep -Eq '^[0-9]+#39;; echo $?0. Quiet grep matched your PID against the digits-only pattern and reported success the Linux way, through its exit code. Nothing printed except the code you asked for. This is the same machinery the && version used; you have just read the raw signal instead of the friendly message.
A fair question at this point: if $ changes all the time, can you trust it inside a single session? Yes. $ is assigned once, when your shell starts, and holds that same number until the shell exits. Ask for it twice on one line and you get the same value both times.
Prove it with the string test from the exit codes lesson. [ "$" = "$" ] asks "are these two equal?" and answers through its exit code:
[ "$" = "$" ]; echo $?
Before you run it, decide: will the code be 0 (equal) or 1 (not equal)?
prompt: student@linuxcamp:~$ answer: [ "$" = "$" ]; echo $? output: 0 hint: The test brackets with $ quoted on both sides, then echo $?: [ "$" = "$" ]; echo $?
0: equal. Both $ expansions produced the same PID, because within one shell session the number never moves. Open a brand new terminal tomorrow and you get a different number, but it will be just as steady for that session. Stable inside, unique across: exactly the two properties that make report-$.tmp a collision-proof file name.
One common mix-up to head off: $ is your shell's PID, not the PID of the command you just ran. The variable that tracks a job you started is $!, and it has its own lesson next.
Scaffolding off. Each box below states a goal; you recall the form and type it yourself. Everything you need appeared earlier in this lesson.
Challenge 1. Prove your shell's PID is all digits and print the confirmation message.
prompt: student@linuxcamp:~$ answer: echo $ | grep -Eq '^[0-9]+
#39; && echo "yes: it is all digits" output: yes: it is all digits hint: The workhorse line from earlier: pipe your PID into the quiet digits check, then chain the echo of the message with &&.Challenge 2. Run the same digit check, but instead of a message, read the result as an exit code.
prompt: student@linuxcamp:~$ answer: echo $ | grep -Eq '^[0-9]+
#39;; echo $? output: 0 hint: Swap the && message for a semicolon and the exit code reader you know from the $? lesson.Challenge 3. Prove on one line that $ gives the same value both times you ask.
prompt: student@linuxcamp:~$ answer: [ "$" = "$" ]; echo $? output: 0 hint: Compare $ to itself with the square-bracket string test, then read the code.
You earned this cheat sheet. Every row is something you just ran or reasoned through:
The one idea to keep: $ is your shell's own process ID. Unique across shells, steady within a session, and never a number you memorize, always a number you use.
Whenever a lesson or a script shows a specific PID, treat it as a stand-in. Yours will differ, and that is correct behavior, not an error. If you catch yourself checking whether your number "matches," step back and ask what property of the number actually matters.
You met $, proved it is a real live number, read the proof both as a message and as an exit code, and confirmed it stays put for the life of your shell. Every one of those you typed yourself and saw the real result.
This module ends with one real Linux machine: the chaining mission lab. There a VM boots just for you, and the mission hands you objectives that combine $ with the operators from the other chaining lessons, with no commands shown. You read the objective, recall the form, and type it. That recall is what turns this reading into a reflex.
Finish the other chaining lessons, then go run it for real.
Practice The $ Shell PID Variable in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.