Naming variables
There are a few rules to keep in mind while naming a variable:
- Variables can begin with an alphanumeric character or underscore, followed by a letter, number, or underscore.
- Variables are case sensitive, so “gfg” and “Gfg” are two different variables.
- Variables cannot start with a number.
- Do not use special characters while defining a variable name.
Example of rightly defined variable names
gfg GFG Gfg _gfg g_f_g March6
Examples of wrongly defined variable names
6March 567 !gfg @Gfg gfg*GFg
Syntax to declare the variable or set its value
Declaring or setting the value of a variable is fairly simple. The basic syntax is as follows:
variableName=value
Example:
myvar=geeks
There should not be any space between variable name, equal sign, and the value assigned. Otherwise, you will encounter an error:
If the value of the variable contains spaces then use quotes to define them.
myvar="Geeks for geeks"
You can also declare a variable using declare command:
declare myvar="geeks"
Bash Script – Define Bash Variables and its types
Variables are an important aspect of any programming language. Without variables, you will not be able to store any required data. With the help of variables, data is stored at a particular memory address and then it can be accessed as well as modified when required. In other words, variables let you store, read, access, and manipulate data.