echo, variables and PATH
The shell keeps named values called variables. Some are yours, made on the spot. Others, the environment variables, are handed to every program you start, and tell it things such as where your home folder is and where to look for commands.
echo
asha@pc:~/demo$ echo Hello from the terminal
Hello from the terminal
asha@pc:~/demo$ echo "Two spaces are kept inside quotation marks"
Two spaces are kept inside quotation marksecho prints its arguments. With > it writes them to a file, and with >> it adds to the end of one.
asha@pc:~/demo$ echo "First line" > new.txt
asha@pc:~/demo$ echo "Second line" >> new.txt
asha@pc:~/demo$ cat new.txt
First line
Second lineDanger. A single > replaces the whole file without asking. Use >> to add.
Variables
Set a variable with a name, an equals sign and a value, with no spaces round the equals sign. Read it with a dollar sign.
asha@pc:~/demo$ city=Ludhiana; echo "I live in $city"
I live in Ludhianacity = Ludhiana, with spaces, is an error: the shell looks for a command named city.
Quotation marks
| Form | Effect |
|---|---|
"double" | variables inside are replaced by their values |
'single' | everything is taken literally |
asha@pc:~/demo$ name=Asha; echo "Hello $name"; echo 'Hello $name'
Hello Asha
Hello $nameThe result of a command
$( ) runs a command and puts its output in place.
asha@pc:~/demo$ echo "There are $(ls | wc -l) items here"
There are 7 items hereThe environment
These are set for you when you sign in.
| Variable | Holds |
|---|---|
$HOME | your home folder |
$USER | your user name |
$PWD | the current folder |
$SHELL | the shell program you are using |
$PATH | the folders searched for commands |
$LANG | your language setting |
asha@pc:~/demo$ echo $PWD
/home/asha/demoenv or printenv lists every environment variable.
export
A plain variable belongs to the shell only. export passes it on to the programs you start from that shell.
export EDITOR=nanoVariables set this way last until you close the terminal.
PATH
When you type a command, the shell looks for a program of that name in each folder listed in PATH, in order, and runs the first one found.
echo $PATH/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinThe folders are separated by colons. If a command is in none of them, you get:
asha@pc:~/demo$ nosuchcommand
/usr/bin/bash: line 1: nosuchcommand: command not found"Command not found" means it is not on the PATH. The program may or may not be installed.
Which program runs?
asha@pc:~/demo$ which ls
/usr/bin/ls
asha@pc:~/demo$ type cd
cd is a shell builtinwhich prints the full path of the program a command would start. type also knows about commands built into the shell and about aliases.
Running a program that is not on the PATH
The current folder is not searched. To run a script in it, give its path, starting with a dot and a slash:
./backup.shThis is deliberate. If the current folder were searched, a file named ls planted in a shared folder could run instead of the real one.
Adding a folder to PATH
For the current session:
export PATH="$PATH:$HOME/bin"To make it permanent, put that same line at the end of your shell's start-up file: ~/.bashrc for Bash on Linux, ~/.zshrc for Zsh, which is the default on the Mac. Then open a new terminal, or run source ~/.zshrc.
Danger. Always include $PATH itself in the new value. export PATH="$HOME/bin", without it, replaces the whole list, and ordinary commands such as ls stop being found. If that happens, close the terminal and open a new one: the change lasted only for that session. If the mistake is in the start-up file, edit the file using a command's full path, such as /usr/bin/nano ~/.zshrc.
Aliases
An alias gives a short name to a longer command.
alias ll='ls -lah'
alias gs='git status'Put aliases in the start-up file to keep them. alias on its own lists them, and unalias ll removes one.
A first script
A script is a text file of commands. The first line names the shell that should run it.
#!/bin/bash
echo "Hello, $USER"
echo "Today is $(date +%A)"Save it as hello.sh, make it runnable with chmod +x hello.sh, and run it with ./hello.sh. See chmod.
Clearing the screen
clear, or Ctrl + L, clears the terminal.
The Windows equivalents are echo, set and PATH.
Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.
This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.