For shell, there are both environment variables and local variables, which are explained as follows.
- Environment Variables: These are variables created during system startup, primarily those created by rootfs to serve the system, and can also include variables imported by users through commands like export. Environment variables can be used in all shell interactive environments and script environments, and can be imported and modified using the export command.
- Local Variables: These are defined within scripts or commands and are only valid in the current shell instance, meaning they are only effective in the current environment and cannot be accessed by other shell scripts.
System Environment Variables
System environment variables are globally present variables in the operating system that provide important configuration information and runtime environments for the system and applications. For example, the addresses for command and system library retrieval, user working directories, etc. In the command line or scripts, the specific value of an environment variable can be obtained in the form of<span><span>$name</span></span>, and the value of the environment variable can also be printed using<span><span>echo $name</span></span>. Additionally, the command printenv can be used to view all environment variables under the current user, or<span><span>printenv $name</span></span> to print specific information.
Environment variables can also be categorized into those serving the file system and user-defined variables, with the main environment variables in the system listed as follows.
| Variable Name | Description |
|---|---|
<span><span>$UID</span></span> |
UID number of the current account |
<span><span>$USER</span></span> |
Username of the current account |
<span><span>$HISTSIZE</span></span> |
Maximum number of historical commands for the current terminal |
<span><span>$HOME</span></span> |
Root directory of the current account |
<span><span>$LANG</span></span> |
Language used in the current environment |
<span><span>$PATH</span></span> |
Command search directory |
<span><span>$PWD</span></span> |
Current working directory |
<span><span>$RANDOM</span></span> |
Randomly returns a number between 0 and 32767 |
<span><span>$PS1</span></span> |
Command prompt, showing the current username, machine name, and current directory name followed by<span><span>$</span></span> symbol |
<span><span>$PS2</span></span> |
Secondary command prompt, used to prompt for subsequent input, usually represented by<span><span>></span></span> character |
For user environment variables, they can be imported into the system using the export command through the command line or shell scripts, as shown below.
# Import USER_VAR variable into the system, only valid for the current environment# To make it persist, it needs to be added to user startup scripts or environments, such as /etc/profile, /etc/environment, .bashrc, etc.export USER_VAR="user test"echo $USER_VAR
<span><span>For variable content, it is generally a string enclosed in quotes, where the type of quotes can be double quotes ("") or single quotes (''). Double quotes ("") indicate a whole, while single quotes ('') will escape special symbols. The quotes in the string can also include backticks (``), which indicate command execution.</span></span>
As shown below.
# String declarationtest="test string"echo "$test" # Outputs test stringecho '$test' # Outputs $test, $ is not recognized as a special symbolTEXT=' $HOME `date` \n'echo "$TEXT" # $HOME `date` \n# Backticks are equivalent to $(cmd), executing the internal cmd's commandDATE=`date`echo "$DATE" # Wed Oct 22 10:40:34 AM CST 2025
Normal Variables
In Linux, another important part is local variables, and the operations for local variables are as follows.
- Create a normal variable, corresponding to
<span><span>name="value"</span></span>format, where there should be no spaces around the = sign. - Create a variable that is only valid within the function body, corresponding to
<span><span>local var="value"</span></span>format, where local can only be used inside functions and can only be accessed internally. - Create a read-only variable, corresponding to
<span><span>readonly var="value"</span></span>format, where readonly variables can only be defined and cannot be modified. - Delete a variable, corresponding to
<span><span>unset var</span></span>format, which cannot be accessed after deletion, but can only be used to delete non-read-only variables. - Read a variable using
<span><span>echo $var</span></span>or<span><span>echo ${var}</span></span>, where<span><span>${var}</span></span>indicates using the value of variable var, and if there is no ambiguity, the braces can be omitted. - String declarations can be concatenated directly, supporting formats like
<span><span>var="str1""str2", var="str1"str2"str3"</span></span>, and single quote mode is also supported. - The syntax
<span><span>${</span><span>#var</span><span>}</span></span>can be used to get the length of a variable. - The syntax
<span><span>${var:offset:length}</span></span>can be used to get a substring of a variable, where offset indicates the starting position of the substring, and length indicates the length of the substring (optional, indicating to take all).
Specific examples are shown below.
var="test"echo $var # Outputs testecho ${var} # Outputs testfunction test(){ local var="local test" echo $var # Outputs local test}# Read-only variablereadonly r_var="readonly test"echo $r_var # Outputs readonly testunset varecho $var # Outputs empty# String concatenation("")varstr_0="This is a connect string"varstr_1="This"" is a connect string"varstr_2="This" is "a connect string"echo $varstr_0echo $varstr_1echo $varstr_2# String concatenation('')varstr_3='This is a connect string'varstr_4='This'' is a connect string'varstr_5='This 'is' a connect string'echo $varstr_3echo $varstr_4echo $varstr_5# Get string lengthecho ${#varstr_0}# Get substringecho ${varstr_0:0:5}echo ${varstr_0:1:4}echo ${varstr_0:5}
Array Variables
Bash supports array variables, with the format<span><span>array=(value1 value2 ...)</span></span> for one-dimensional arrays. Additionally, elements of the array can be modified using<span><span>array[index]=value</span></span>, and if the array does not exist, it will create the corresponding element of the array.
- Access array elements using
<span><span>echo ${array[index]}</span></span>format. - Access all elements of the array using
<span><span>echo ${array0[*]}</span></span>and<span><span>echo ${array[@]}</span></span>format. - Get the number of elements in the array using
<span><span>echo ${#array[*]}, echo ${#array[@]}, echo ${#array}, echo ${#array[0]}</span></span>.
Specific examples are shown below.
# Define arrays array0 and array1array0=(A B C D)array1[0]=Aarray1[1]=B# Access elements of the associative array in ${array[index]} formatecho ${array0[0]}# Get all elements in the arrayecho ${array0[*]}echo ${array0[@]}# Get the length of the arrayecho ${#array0[*]}echo ${#array0[@]}echo ${#array0}echo ${#array0[0]}# Calculate Fibonacci sequence num=12fibo=(1 1)for ((i=2; i<$num; i++))do let fibo[$i]=fibo[$i-1]+fibo[$i-2]doneecho ${fibo[@]}
Associative Array Variables
For bash shell, associative arrays similar to dictionaries are also supported. The format for associative arrays is<span><span>declare -A array=([key]=value [key]=value ...)</span></span>.
- Assign values to associative arrays using
<span><span>array[key]=value</span></span>. - Access elements of associative arrays using
<span><span>echo ${array[key]}</span></span>format.
Specific examples are shown below.
# Define associative arraysdeclare -A site0=([