Linux shell programming study notes 32: declare command

0 Preface

exist

Linux shell programming study notes 16: Associative arrays in bashicon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/134053506?spm=1001.2014.3001.5501< /span>

, we use the declare command when defining an associative array.

In fact, the function of declare command is not only to define associative array, now we will explore its function and usage.

1 Function and format of declare command

We can use the command help declare  to view the function and format of

purpleEndurer @ bash $ help declare 
declare: declare [-aAfFgilrtux] [-p] [name[=value] ...]
    Set variable values and attributes.
    
    Declare variables and give them attributes.  If no NAMEs are given,
    display the attributes and values of all variables.
    
    Options:
      -f        restrict action or display to function names and definitions
      -F        restrict display to function names only (plus line number and
        source file when debugging)
      -g        create global variables when used in a shell function; otherwise
        ignored
      -p        display the attributes and value of each NAME
    
    Options which set attributes:
      -a        to make NAMEs indexed arrays (if supported)
      -A        to make NAMEs associative arrays (if supported)
      -i        to make NAMEs have the `integer' attribute
      -l        to convert NAMEs to lower case on assignment
      -r        to make NAMEs readonly
      -t        to make NAMEs have the `trace' attribute
      -u        to convert NAMEs to upper case on assignment
      -x        to make NAMEs export
    
    Using `+' instead of `-' turns off the given attribute.
    
    Variables with the integer attribute have arithmetic evaluation (see
    the `let' command) performed when the variable is assigned a value.
    
    When used in a function, `declare' makes NAMEs local, as with the `local'
    command.  The `-g' option suppresses this behavior.
    
    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

 1.1 Function

The function of declare command is to view or set variable values ​​and attributes; to view the name and definition of custom functions.
    

1.2 Format

declare [-aAfFgilrtux] [-p] [variable name[=value] ...]

Option description:

Options Function description Remark
-a Make specified variable name an indexed array (if supported) Attribute options, indexed arrays
-A Make specified variable name an associative array (if supported) Attribute options, ssociative arrays
-f Limit action or display to function names and definitions function
-F Limit display to only function name (plus line number and source file when debugging) function
-g Create global variables when used in shell functions; otherwise ignored global
-i Make the specified variable name have the "integer" attribute attribute options, integer
-l Convert the specified variable name to lowercase when assigning Attribute options, lower case
-p Display the attributes and values ​​of each specified variable name display
-r  Make the specified variable name read-only Attribute options, readonly
-t Make the specified variable name have the "trace" attribute Attribute options, trace
-in Convert the specified variable name to uppercase when assigning Attribute options, upper case
-x Make the specified variable name exported Attribute options, export

For attribute options, "-" can be used to specify the attributes of the variable, and "+" can cancel the attributes set by the variable.

  • Note: If multiple variables are operated at the same time, separate the variable names with spaces.

2 Usage and examples

2.1 declare: Display user’s shell variables (local variables and environment variables) and custom functions

purpleEndurer @ bash $ declare | more
BASH=/usr/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
COLUMNS=144
DIRSTACK=()
EUID=1000
GROUPS=()
HISTFILE=/home/csdn/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/home/csdn
HOSTNAME=edu
HOSTTYPE=x86_64
IFS=$' \t\n'
LC_ALL=zh_CN.UTF-8
LESS=-R
LINES=32
LOGNAME=csdn
LSCOLORS=Gxfxcxdxbxegedabagacad
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41
:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;3
1:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=
01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=0
1;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=0
--More--

LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41
:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;3
1:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=
01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=0
1;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=0
1;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.
mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;
35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01
;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav
=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:'
MACHTYPE=x86_64-redhat-linux-gnu
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PAGER=less
PATH=/home/csdn/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PIPESTATUS=([0]="0")
PPID=50
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
PS1='purpleEndurer @ bash $ '
PS2='> '
PS4='+ '
PWD=/home/csdn
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
TERM=xterm
UID=1000
ZSH=/home/csdn/.oh-my-zsh
_=declare
colors=/home/csdn/.dircolors
purpleEndurer @ bash $  

Since the current user does not have a custom function, currently only the variable information can be seen.

 

2.2 declare -i variable name 1 [= value 1] ...: declare integer type variable

Example: Declare integer variables a and b, initialize their values ​​to 1 and 2 respectively, then display the values ​​​​of a and b, and then display the sum of a and b

purpleEndurer @ bash $ declare -i a=1 b=2
purpleEndurer @ bash $ echo $a,$b
1,2
purpleEndurer @ bash $ echo $[ a + b ]
3

2.3 declare + attribute variable name: cancel variable attribute

Example: Declare integer variables a and b, initialize their values ​​to 1 and 2 respectively, and then assign the value "a" to a.

purpleEndurer @ bash $ declare -i a=1 b=1 #statement integer type change a sum b, its first beginning its division 1sum 2
purpleEndurer @ bash $ a='a' b='b'
< a i=5>     #给变鵋值'a'、变变鵋值'b' purpleEndurer @ bash $ echo $a $b #showa和b的值
1 1
purpleEndurer @ bash $ declare +i a #Integer attribute for cancellation amount a
purpleEndurer @ bash $ a= 39;a' b='b'
purpleEndurer @ bash $ echo $a $b #showa和b的值
a 1
purpleEndurer @ bash $ 

It can be seen that after declaring integer variables a and b and initializing their values ​​to 1 and 2 respectively, it is invalid to directly assign string values ​​to integer variables a and b.

You need to cancel the integer attribute of variable a before you can assign the string value 'a' to variable a.

 2.4 declare -r variable name[=value]......: define read-only variables

Define a read-only integer variable c and initialize it to 1

purpleEndurer @ bash ~ $ declare -ri c=1
purpleEndurer @ bash ~ $ echo $c
1
purpleEndurer @ bash ~ $ c=2
bash: c: readonly variable
purpleEndurer @ bash ~ $ declare +r c
bash: declare: c: readonly variable

The value of a read-only variable cannot be changed.

You cannot use declare +r to remove the read-only attribute of variable c.

2.5 [declare -a] Index array name [= (value 1 value 2...)]: Define index array

Example: Define and initialize the index array a, and then display its value: 

purpleEndurer @ bash ~ $ declare -a a=(1 'a' b)
purpleEndurer @ bash ~ $ echo ${a[*]}
1 a b

For more knowledge about indexed arrays, you can review:

 Linux shell programming study notes 15: Define arrays, obtain array element values ​​and lengths, and splice or merge arraysicon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details /134009982?spm=1001.2014.3001.5501

 

2.6 declare -A associative array name [= ([string subscript 1] = value 1 [string subscript 2] = value 2 ... [string subscript n] = value n)]: Define associative array

Example: Define and initialize the associative array b, and then display its value: 

purpleEndurer @ bash ~ $ declare -A b=(["apple"]=1 ["boy"]=2 ["cat"]=3)
purpleEndurer @ bash ~ $ echo ${b[*]}
3 1 2

For more knowledge about associative arrays, you can review:

Linux shell programming study notes 16: Associative arrays in bashicon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/134053506?spm=1001.2014.3001.5501< /span>

2.7 declare -f[function name]: Display the specified user-defined function name and definition

Example: Define functions a and b, and then use the declare -f command to view their names and definition codes

purpleEndurer @ bash ~ $ function a(){ echo $0;}
purpleEndurer @ bash ~ $ a
bash
purpleEndurer @ bash ~ $ function b(){ echo $#;}
purpleEndurer @ bash ~ $ b
0
purpleEndurer @ bash ~ $ declare -f
a () 

    echo $0
}
b () 

    echo $#
}
purpleEndurer @ bash ~ $ declare -f a
a () 

    echo $0
}
purpleEndurer @ bash ~ $ declare -f b
b () 

    echo $#
}
purpleEndurer @ bash ~ $ 

 

If no function name is specified when using the declare -f command, all function names and definition codes will be displayed.

2.8 declare -F[function name]: Display the specified user-defined function name

Example: We use the declare -F command to view the names of the custom functions a and b in the example in 2.7

purpleEndurer @ bash ~ $ declare -F
declare -f a
declare -f b
purpleEndurer @ bash ~ $ declare -F a
a
purpleEndurer @ bash ~ $ declare -F b
b
purpleEndurer @ bash ~ $ 

Similar to the declare -f command, if no function name is specified when using the declare -F command, all function names will be displayed.

3 Comparison between declare command and set command in displaying shell variables

set : Display the user’s local variables and user environment variables.

declare: Like set, displays the user's shell variables (local variables and environment variables)

Guess you like

Origin blog.csdn.net/Purpleendurer/article/details/134767745