How to Create a Variable in the Terminal
A variable lets you type a long value once and reuse it: a version number, a folder, a web address. In zsh and bash you create one with NAME='value' and use it as $NAME. PowerShell writes it as $name = 'value'. This guide does both, with real output.
Syntax
In zsh and bash:
NAME='value'
echo "$NAME"
echo "${NAME}s"
| Part | What it means |
|---|---|
NAME='value' | Creates the variable. No spaces around = |
$NAME | Its value, wherever you write it |
${NAME} | The same, with braces marking where the name ends |
Double quotes around "$NAME" | Keep a value with spaces together as one piece |
The zsh manual calls these parameters: "A parameter has a name, a value, and a number of attributes."
In PowerShell:
$name = 'value'
"$name"
"${name}s"
Microsoft's documentation says: "To create a new variable, use an assignment statement to assign a value to the variable. You don't have to declare the variable before using it."
The Example
The example stores the name Ada and uses it in two greetings. The second greeting adds an s right after the name. In zsh it then starts a separate program and asks it for the variable.
Step 1: Create and Use the Variable
- macOS / Linux
- Windows (PowerShell)
NAME='Ada'
echo "Hello, $NAME"
echo "Hello, ${NAME}s"
zsh -c 'echo "Child program sees: [$NAME]"'
Hello, Ada
Hello, Adas
Child program sees: []
$name = 'Ada'
"Hello, $name"
"Hello, ${name}s"
Hello, Ada
Hello, Adas
![The zsh terminal after the four commands: Hello, Ada, then Hello, Adas, then Child program sees: [].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/shell/2026/shell-variable-01-zsh.webp)
$NAME became Ada, and ${NAME}s put the s right after it. The last line shows the limit of a plain variable: the separate zsh program started by the command got nothing, because the variable belongs only to your terminal. To hand a value to programs you start, use export and make it an environment variable.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Spaces around =: NAME = 'Ada' | The shell treats NAME as a command: command not found: NAME in zsh, NAME: command not found in bash | NAME='Ada', with no spaces |
$NAMEs when you mean the name plus an s | The shell looks for a variable named NAMEs, finds none, and prints nothing: Hello, | ${NAME}s |
| Expecting a program you start to see the variable | It sees nothing, as the example showed | export NAME |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
command not found naming your variable | Spaces around = | Remove them |
| The value is empty where you used it | A typo in the name, or the name runs into the next letters | Check the spelling, and use ${NAME} |
FAQ
What's the difference between a variable and an environment variable?
A variable lives only in your terminal. An environment variable, made with export (or $env: in PowerShell), is also passed to every program the terminal starts.
Do I need quotes around the value?
Only when it has spaces or special characters, but quoting always is a good habit: NAME='Ada Lovelace'.
Conclusion
NAME='Ada' made a variable that $NAME and ${NAME}s used in text, while a program started from the terminal couldn't see it. Leave no spaces around =, and use braces where the name runs into other text.
Additional Resources
- zsh parameters — variables in zsh.
- about_Variables — variables in PowerShell.
