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. 

Similar Reads

Working of Bash variables

When you write a bash script, you either want to manipulate the already defined variable, set the value of the variable, or simply read the predefined variables. There are various ways to set values of a variable:...

Naming variables

There are a few rules to keep in mind while naming a variable:...

Using Local and global variables in bash

When you define a variable in your shell, it is only accessible during that particular session. Because that is a local variable. In the below example you can see that when we tried to access the variable “var1” in the new session (session number-1692), it does not print our variable value....

Types of bash variables

There are two types of variables in bash:...

Built-in special variables

Bash comes with some built-in special variables. These variables store some important data that can come in handy when you write a shell script. They are available for use to all users. Below is the list of these special variables and the result they store:...

Using array variables in Bash

Arrays are used to store data and in bash, you can store values of different types or the same type in an array....

Explicitly declaring datatype of a variable

In bash, we do not need to declare data types, like any other programming languages (int, str, etc..). The data type is identified by the shell script/shell itself (the variables defined in bash are termed as untyped). But there can be some circumstances when you will want to explicitly define the data type. In such cases, you can utilize the declare command to create a variable with a particular data type. Declare commands offer many flags to explicitly declare data types. Below is the list of those flags:...

Command-line arguments

Command-line arguments or positional parameters are used to pass input to a shell script or a command. Using command-line arguments makes the script more dynamic....

Command substitution

Sometimes, while writing a shell script you will encounter some instances when you want to assign the output of a particular command to a variable. This can be done with the help of command substitution.  Command substitution typically works by running a particular shell command and storing its result in a variable for further use or to display....