Using variables

The basic form of a variable assignment in shell script looks like this:

myshell='GNU Bourne-Again shell'

This declares a variable named myshell, and gives it the string contents GNU Bourne-Again shell.

Note that there is no space either before or after the equals sign, as there might be in a C-like language. Note also that the value in this example assignment is surrounded by single quotes, so the contents are interpreted literally, without expanding any special characters, in this case spaces. As with other single-quoted strings in shell script, the quotes themselves do not become part of the value.

You will need to quote any value that contains special characters in variable assignments. For new shell programmers, it is ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.