TExcelTAD
Home › Terminal & Command Line › The Prompt & Command Syntax

The Prompt & Command Syntax

When a shell is ready for input it prints a prompt. Everything you type after it is a command line, and nearly every command line in every shell has the same shape: a command name, then options, then arguments. This lesson teaches you to read both.

Reading the prompt

The default prompts tell you who and where you are:

text
C:\Users\ada>                 CMD: current folder, then >
PS C:\Users\ada>              PowerShell: PS, current folder, then >
ada@ubuntu:~$                 bash: user@host:folder, then $
ada@macbook ~ %               zsh on macOS: user@host folder, then %
root@ubuntu:/etc#             bash as root (administrator): # instead of $
  • ~ (tilde) is short for your home folder, so ada@ubuntu:~/projects$ means /home/ada/projects.
  • On Linux and macOS, $ or % means a normal user and # means root. Be careful at a # prompt.
  • Windows doesn't change the prompt for an admin shell; look at the window title (Administrator:).

Note: In tutorials (including this one) the prompt is printed in front of the command to show where it is typed. Don't type the prompt itself: in ada@ubuntu:~$ ls -l, you type only ls -l.

The shape of a command

text
  command   options          arguments
  ───────   ──────────────   ─────────────────────
  ls        -l -a            Documents Downloads        bash / zsh
  dir       /b /o:n          Documents                  CMD
  Copy-Item -Recurse         -Path src -Destination bak PowerShell
  • The command is a built-in of the shell (cd, echo) or the name of a program (git, ping, python).
  • Options (also called flags or switches) change how the command works.
  • Arguments are what it works on: file names, text, URLs.
  • Parts are separated by spaces. An argument that itself contains spaces must be quoted: "My Documents".

How options are written

Shell Style Examples
CMD slash + letter, often case-insensitive dir /b, del /q, findstr /i, dir /o:n
PowerShell dash + full parameter name (may be shortened while unambiguous) -Recurse, -Path x, -Force; -rec also works
bash / zsh (GNU tools) one dash + one letter, or two dashes + a word -l, -a, -la (combined), --all, --color=never
macOS (BSD tools) mostly single-letter options ls -la, sed -E (few --long options)

Short Unix options can be combined: ls -l -a and ls -la are the same. Long options are easier to read in scripts: --recursive says more than -r.

Options in action

Let's create a few files and list them with and without options.

Windows (CMD)

cmd · try it yourself
@echo off
mkdir demo
cd demo
echo one> b.txt
echo two> a.txt
mkdir logs
dir /b /o:n
echo ---
dir /b /o:n /a:d
a.txt
b.txt
logs
---
logs

/b means bare (names only), /o:n order by name, /a:d only directories.

Windows (PowerShell)

powershell · try it yourself
New-Item -ItemType Directory demo | Out-Null
Set-Location demo
'one' | Set-Content b.txt
'two' | Set-Content a.txt
New-Item -ItemType Directory logs | Out-Null
Get-ChildItem -Name
'---'
Get-ChildItem -Directory -Name
logs
a.txt
b.txt
---
logs

PowerShell lists folders first. -Name returns only names, and -Directory keeps only folders.

Linux & macOS (bash/zsh)

bash · try it yourself
mkdir demo && cd demo
echo one > b.txt
echo two > a.txt
mkdir logs
ls
echo ---
ls -F
echo ---
ls -d */
a.txt
b.txt
logs
---
a.txt
b.txt
logs/
---
logs/

-F adds a / after folder names. ls -d */ lists only directories. Note that ls prints one name per line when its output goes to a file or pipe (like here), and in columns in a real terminal.

Case sensitivity

Windows shells don't care about upper or lower case in command names; Unix shells do:

cmd · try it yourself
@echo off
ECHO Shouting works in CMD
Echo so does this
Shouting works in CMD
so does this
powershell · try it yourself
WRITE-OUTPUT 'PowerShell is case-insensitive too'
(GET-ITEM -PATH .).Name   # parameter names too
PowerShell is case-insensitive too
tmp
bash · try it yourself
echo "lower case works"
ECHO "upper case does not"
echo "exit status: $?"
lower case works
/w/main.sh: line 2: ECHO: command not found
exit status: 127

Exit status 127 is the standard Unix code for "command not found". The same rule applies to file names on Linux (Notes.txt and notes.txt are different files), while Windows and (by default) macOS treat them as the same name.

Customising the prompt

Every shell lets you change its prompt. The setting goes in your profile file so it applies to every new window (Aliases & Profiles).

Windows (CMD)

CMD's prompt is the PROMPT variable. Codes like $P (path) and $G (>) are expanded each time:

cmd · try it yourself
@echo off
echo Default prompt code: %PROMPT%
prompt [$P]$G
echo New prompt code: %PROMPT%
Default prompt code: $P$G
New prompt code: [$P]$G

An interactive window would now show [C:\Users\ada]>.

Windows (PowerShell)

PowerShell's prompt is a function named prompt; whatever it returns is printed:

powershell · try it yourself
function prompt { "[$env:USERNAME] " + (Split-Path -Leaf (Get-Location)) + '> ' }
$env:USERNAME = 'ada'
prompt
[ada] tmp> 

Linux (bash)

bash uses the PS1 variable with backslash codes: \u user, \h host, \w folder, \$ ($ or # for root). ${PS1@P} expands it the way the prompt would be shown:

bash · try it yourself
PS1='\u@\h:\w\$ '
echo "${PS1@P}"
PS1='[\W] > '
echo "${PS1@P}"
learner@tad:~$ 
[~] > 

macOS (zsh)

zsh uses PROMPT (or PS1) with percent codes: %n user, %m host, %~ folder, %# (% or # for root). print -P expands them:

zsh · try it yourself
PROMPT='%n@%m %1~ %# '
print -P "$PROMPT"
learner@tad ~ % 
Item CMD PowerShell bash zsh
Prompt setting PROMPT variable / prompt command prompt function PS1 PROMPT / PS1
Current folder $P Get-Location \w (\W last part) %~ (%1~ last part)
User / host %USERNAME% / %COMPUTERNAME% $env:USERNAME \u / \h %n / %m
Saved in AutoRun registry value (rare) $PROFILE ~/.bashrc ~/.zshrc

Learn more: CMD Syntax · Bash Syntax · PowerShell First Commands

Test yourself

What does a # at the end of a Linux prompt usually mean?

  1. A comment follows
  2. You are in your home folder
  3. You are logged in as root
  4. The last command failed

Which command is the same as ls -l -a?

  1. ls -la
  2. ls --la
  3. ls /l /a
  4. ls -l,a

In which shell does ECHO hello fail with "command not found"?

  1. CMD
  2. PowerShell
  3. bash
  4. None of them