ShareYourGroupShareYourGroupCommands

Linux and Mac commands

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

Linux or Mac terminal
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 marks

echo prints its arguments. With > it writes them to a file, and with >> it adds to the end of one.

Linux or Mac terminal
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 line

Danger. 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.

Linux or Mac terminal
asha@pc:~/demo$ city=Ludhiana; echo "I live in $city"
I live in Ludhiana

city = Ludhiana, with spaces, is an error: the shell looks for a command named city.

Quotation marks

FormEffect
"double"variables inside are replaced by their values
'single'everything is taken literally
Linux or Mac terminal
asha@pc:~/demo$ name=Asha; echo "Hello $name"; echo 'Hello $name'
Hello Asha
Hello $name

The result of a command

$( ) runs a command and puts its output in place.

Linux or Mac terminal
asha@pc:~/demo$ echo "There are $(ls | wc -l) items here"
There are 7 items here

The environment

These are set for you when you sign in.

VariableHolds
$HOMEyour home folder
$USERyour user name
$PWDthe current folder
$SHELLthe shell program you are using
$PATHthe folders searched for commands
$LANGyour language setting
Linux or Mac terminal
asha@pc:~/demo$ echo $PWD
/home/asha/demo

env 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.

Linux or Mac terminal not run here
export EDITOR=nano

Variables 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.

Linux or Mac terminal not run here
echo $PATH
What the output looks like an illustration, not a real run
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The folders are separated by colons. If a command is in none of them, you get:

Linux or Mac terminal
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?

Linux or Mac terminal
asha@pc:~/demo$ which ls
/usr/bin/ls
asha@pc:~/demo$ type cd
cd is a shell builtin

which 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:

Linux or Mac terminal not run here
./backup.sh

This 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:

Linux or Mac terminal not run here
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.

Linux or Mac terminal not run here
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.

Linux or Mac terminal not run here
#!/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.