Running Your First Bash Command
A Bash command is a line of text that asks the shell to do one job: print a message, show the date, list files, or start a program. This lesson walks through your first few commands, explains what Bash does between you pressing Enter and seeing the result, and shows how to read the status that every command leaves behind.
Understanding the Concept
Every command you type is a request to run something. When you press Enter, Bash reads the line, splits it into words, and treats the first word as the command name. The remaining words are extra information for that command. Bash then finds the program or built-in feature that matches the name, runs it, and prints whatever the command produces.
When the command ends, Bash also records a number called the exit status. That number tells you whether the command succeeded. You will rely on it heavily when you write scripts.
How Bash Runs a Command
The sequence for one line looks like this:
- You type the line and press
Enter. - Bash splits the line into words, using spaces as separators.
- Bash looks up the first word. It may be a feature built into Bash itself, or a program stored somewhere on disk. Lesson 14 explains the difference.
- Bash runs it and passes the other words along.
- The output is printed in the terminal.
- Bash stores the exit status and shows a new prompt.
In the examples below, the prompt is shown as $ and you type only the text after it.
Your First Command: echo
The echo command prints the words you give it:
echo Hello
Output:
Hello
Here echo is the command name and Hello is the information you pass to it. The output appears on the next line, and then Bash shows a new prompt.
To print a sentence with punctuation or several spaces, put the text in quotes:
echo "Hello, World"
Output:
Hello, World
The quotes tell Bash to treat everything between them as one piece of text. Without quotes, Bash splits the line at the spaces, so extra spaces are lost:
echo Hello, World
Output:
Hello, World
Quoting is covered in detail in a later section, but you can already see why it matters.
Asking the System Questions
Many short commands report facts about your system. Each one runs and prints its result:
whoami
Sample output:
student
whoami prints the name of the user running the shell.
pwd
Sample output:
/home/student
pwd stands for "print working directory". It shows the directory the shell is currently in.
date
Sample output:
Thu Nov 20 10:52:07 UTC 2026
date prints the current date and time as your system sees it. The date format depends on your language settings. You can ask for a specific format by adding a pattern that starts with +:
date +%Y-%m-%d
Sample output:
2026-11-20
Here %Y means the four-digit year, %m the month, and %d the day of the month.
uname -s
Output on Linux:
Linux
uname -s prints the name of the operating system kernel. On macOS the answer is Darwin.
Your output differs from the samples because it depends on your user name, directory, and clock. That is expected.
Listing Files with ls
The ls command lists the files and directories in the current directory:
ls
Sample output in a directory with two files:
notes.txt report.txt
If the directory is empty, ls prints nothing and returns directly to the prompt. Empty output is not an error.
Doing Arithmetic
Bash can calculate integers with a feature called arithmetic expansion, written $(( )):
echo $((2 + 3 * 4))
Output:
14
Bash evaluates the expression inside the double parentheses using normal operator precedence, so the multiplication happens before the addition. It replaces the whole $((...)) with the result, and echo prints it. The details of arithmetic come later in the tutorial.
Formatting Output with printf
The printf command prints text using a template, which gives you more control than echo:
printf 'Name: %s, Age: %d\n' Asha 30
Output:
Name: Asha, Age: 30
The template is the first argument. The marker %s is replaced by the next text value, %d by the next whole number, and \n adds a line break at the end. Unlike echo, printf does not add a newline for you.
Understanding Exit Status
After each command, Bash stores a number from 0 to 255 in the special variable $?. By convention:
0means the command succeeded.- Any other number means something went wrong, and different numbers can describe different problems.
Check it right after a command:
true
echo "status=$?"
Output:
status=0
The true command does nothing and always succeeds. The command false does nothing and always fails:
false
echo "status=$?"
Output:
status=1
A real failure produces an error message as well. Asking ls to list a directory that does not exist gives:
ls /nonexistent_dir
echo "status=$?"
Output:
ls: cannot access '/nonexistent_dir': No such file or directory
status=2
The first line is the error message from ls. The second line shows that its exit status was 2. Notice that $? always describes the most recent command, so the value changes every time you run something new. Because echo is also a command, it sets $? to 0 as soon as it finishes. Read the value immediately after the command you care about, or copy it into a variable first.
When the Command Does Not Exist
If Bash cannot find the first word anywhere, it prints an error and sets the status to 127:
foo
echo "status=$?"
Output in an interactive shell:
bash: foo: command not found
status=127
This message appears most often because of a spelling mistake, because the program is not installed, or because the program's directory is not in your PATH. Commands are also case-sensitive, so LS is not the same as ls, and Bash reports LS: command not found.
Running More Than One Command on a Line
You can put several commands on one line when you separate them with a semicolon:
echo "First"; echo "Second"
Output:
First
Second
Bash runs the first command to completion and then runs the second. Semicolons and the other ways of joining commands have their own lesson later.
Putting the Commands Together
The following short session combines the commands from this lesson. Each line is typed at the prompt:
echo "Starting my first session"
whoami
date +%Y-%m-%d
echo $((6 * 7))
ls /nonexistent_dir
echo "last status: $?"
Sample output:
Starting my first session
student
2026-11-20
42
ls: cannot access '/nonexistent_dir': No such file or directory
last status: 2
Here is how it runs:
echoprints the greeting.whoamiprints your user name, which will differ from the sample.date +%Y-%m-%dprints today's date in year-month-day order.echo $((6 * 7))evaluates the arithmetic and prints42.ls /nonexistent_dirfails and prints an error message.- The final
echoreads$?and reports the failedlsstatus,2, becauselswas the last command to run before it.