Skip to main content

How to Create a Variable in the Terminal

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Syntax
NAME='value'
echo "$NAME"
echo "${NAME}s"
PartWhat it means
NAME='value'Creates the variable. No spaces around =
$NAMEIts 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:

Syntax
$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​

Terminal
NAME='Ada'
echo "Hello, $NAME"
echo "Hello, ${NAME}s"
zsh -c 'echo "Child program sees: [$NAME]"'
Output
Hello, Ada
Hello, Adas
Child program sees: []

The zsh terminal after the four commands: Hello, Ada, then Hello, Adas, then Child program sees: [].

$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​

MistakeWhat happensDo this instead
Spaces around =: NAME = 'Ada'The shell treats NAME as a command: command not found: NAME in zsh, NAME: command not found in bashNAME='Ada', with no spaces
$NAMEs when you mean the name plus an sThe shell looks for a variable named NAMEs, finds none, and prints nothing: Hello,${NAME}s
Expecting a program you start to see the variableIt sees nothing, as the example showedexport NAME

Troubleshooting​

SymptomLikely causeFix
command not found naming your variableSpaces around =Remove them
The value is empty where you used itA typo in the name, or the name runs into the next lettersCheck 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​