Shell from zero: a variable holds text
A shell variable holds text. item=latte stores latte, and echo prints it. There are no spaces around the equals sign.
Zara is one of GoByte's characters. This post was drafted by AI agents in Zara's voice, then fact checked, run and edited by the GoByte team.
The shell stores text. item=latte puts the characters latte in item. echo prints them. There is no int until you ask for arithmetic, and that is a later lesson. A price you type is still text until then.
The value is text, and the spaces matter#
item=latte is a single word. The shell treats it as an assignment because there is no space around =. item = latte is a command named item, and the shell looks for a program by that name. The stored value is the characters of the drink, not a string object and not an int. "$item" expands to that text as one word. An unset name expands to empty unless set -u is on, in which case the expansion is an error. A child process does not see the variable unless you export it. echo is a builtin, so it sees it anyway.
- itemtextlatte
item=latte
echo "$item"
latte
The equals sign is the assignment#
item=latte is one word to the shell. No spaces. item = latte is a command named item, which you almost certainly do not have, and the shell reports that the command was not found. The spaces changed the meaning from assignment to a program launch.
echo "$item" prints the drink. The quotes matter more in the next lesson. They are already here so the habit is in place. Without a previous assignment, $item is empty and echo prints a blank line. An unset variable is not an error unless you turned that option on.
Export when a child program must see it#
item=latte is visible to the rest of this script and to the next lines you type in this shell. A program you start does not see it unless you export item. echo is a builtin and sees it either way. The next lesson shows why the quotes around the expansion are doing work, using a drink name that contains a space.
Your product here? Partner with us
Back to topDiscussion
No comments yet. Signed in GoByte members with a verified e-mail can join. Community guidelines
Reading is open to everyone. Commenting and voting need a GoByte account with a verified e-mail.