- Published on
declare command-declare shell variables
Jonas
1 min read
The declare command is used to define shell variables, but the variables defined by declare can only work in the current shell environment, and will be invalid after switching the shell environment.
Syntax: declare [parameter] [directory]
Command parameters
-a | Declare array variables |
-f | Only show functions |
-F | Do not display function definitions |
-i | First calculate the expression and assign the result to the declared variable |
-p | Display the defined method and value of a given variable. When this option is used, other options will be ignored |
-r | Define read-only variables |
-x | Convert the specified Shell variables into environment variables |
Example
Display the defined variables:
[root@linuxstar ~]# declare
Define a new Shell variable:
[root@linuxstar ~]# declare var1="hello"
Use "-i" to define a new variable:
[root@linuxstar ~]# declare -i var2=100+200
Use "-p" to display the definitions of variables var1 and var2:
[root@linuxstar ~]# declare -p var1 var2
Show all environment variables:
[root@linuxstar ~]# declare -x