Shell variables of linux study notes (2)

1. Shell variables

1) The basic format of defining variables

Variable name = value
Note that there cannot be a space between the variable name and the equal sign, which may be different from the familiar programming languages ​​such as JAVA and C. At the same time, the naming of variable names must follow the following rules:
the first character must be a letter (az, AZ).
There can be no spaces in between, you can use an underscore (_).
Punctuation marks cannot be used.
You cannot use keywords in bash (reserved keywords can be viewed with the help command).

 2) Use variables

Define a variable
variable name=value
There are two ways to get the value of the variable:
a) $variable name
b) ${variable name}
#!/bin/bash
a=10
echo $a
echo ${a}

 3) Readable variables

The format of the readable variable:
readonly variable name
(Note: adding this keyword before the variable name indicates that the variable is read-only and immutable)
#!/bin/bash
a=10
echo $a
echo ${a}
readonly a
a=100
echo $a
Execute script error
[imix@localhost shell]$ ./lsSh.sh
10
10
./lsSh.sh: line 6: a: read-only variable
10

 4) delete variable

The format of deleting a variable:
unset variable name
(Note: add unset before the variable name, the variable is undefined after this sentence, the unset command cannot delete read-only variables)
#!/bin/bash
a=10
echo $a
echo ${ a}
#readonly a
unset a
echo $a (because the variable has been deleted, this sentence will output nothing)

 5) Commonly used variable types (numeric, string, array, environment variables)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326863496&siteId=291194637