Published on

declare command-declare shell variables



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
-aDeclare array variables
-fOnly show functions
-FDo not display function definitions
-iFirst calculate the expression and assign the result to the declared variable
-pDisplay the defined method and value of a given variable. When this option is used, other options will be ignored
-rDefine read-only variables
-xConvert the specified Shell variables into environment variables

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