Linux for Developers Course
Linux for Developers
/
Beginner

Variables

Definition

Temporary storage locations in a Bash script used to hold data (like strings or numbers) that can be referenced or changed later in the script.

Explain Like I'm New

A labeled box. You put the word 'Alice' into a box labeled 'NAME'. Later in the script, you tell the computer to 'Print NAME', and it prints 'Alice'.

Real World Example

In a script: `BACKUP_DIR="/var/backups"` `cp database.sql $BACKUP_DIR` If the company changes their backup folder location, the developer only has to change the variable at the top of the script once, rather than hunting through 500 lines of code.

Common Use Cases

  • •Making scripts reusable
  • •Storing dynamic paths

Interview Questions

basic

  • In Bash, why will `NAME = 'Alice'` (with spaces around the equals sign) cause a massive error?

intermediate

  • What is the difference between single quotes (`'`) and double quotes (`"`) when using variables?

Flash Cards

Question

Why error with spaces?

Click to reveal answer
Answer

Bash is hyper-sensitive to spaces. If you add spaces, Bash assumes `NAME` is a command you are trying to execute, not a variable assignment. It must be strictly `NAME='Alice'`.

Question

Single vs Double quotes?

Click to reveal answer
Answer

Double quotes *evaluate* variables. `echo "Hello $NAME"` prints 'Hello Alice'. Single quotes are *literal*. `echo 'Hello $NAME'` strictly prints the literal text 'Hello $NAME'.