YouTip LogoYouTip

Linux Comm Declare

# Linux declare Command [![Image 3: Linux Command Manual](#) Linux Command Manual](#) The Linux `declare` command is used to declare shell variables. `declare` is a shell command. In the first syntax, it can be used to declare variables and set their attributes (`` represents the variable's attributes). In the second syntax, it can be used to display shell functions. If no parameters are added, it will display all shell variables and functions (the same effect as executing the `set` command). ### Syntax `declare [+/-]` or `declare -f` **Parameter Description**: * +/- "-" can be used to set the variable's attribute, "+" is used to remove the set attribute of the variable. * -f Display functions only. * r Set the variable to read-only. * x The specified variable becomes an environment variable, available for use by programs outside the shell. * i can be a number, string, or expression. ### Examples Declare an integer variable ## Example # declare -i ab //Declare an integer variable # ab=56 //Change the variable content # echo $ab //Display the variable content 56 Change variable attribute ## Example # declare -i ef //Declare an integer variable # ef=1 //Assign a value to the variable (integer) # echo $ef //Display the variable content 1 # ef="wer" //Assign a value to the variable (text) # echo $ef 0 # declare +i ef //Remove the variable attribute # ef="wer" # echo $ef wer Set variable to read-only ## Example # declare -r ab //Set the variable to read-only # ab=88 //Change the variable content -bash: ab: readonly variable # echo $ab //Display the variable content 56 Declare an array variable ## Example # declare -a cd='(="a" ="b" ="c")' //Declare an array variable # echo ${cd} b //Display the variable content # echo ${cd[@]} //Display the entire array variable content a b c Display functions ## Example # declare -f command_not_found_handle () { if[-x/usr/lib/command-not-found ]; then /usr/bin/python /usr/lib/command-not-found --$1; return$?; else if[-x/usr/share/command-not-found ]; then /usr/bin/python /usr/share/command-not-found --$1; return$?; else return 127; fi; fi } [![Image 4: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm DepmodLinux Comm Crontab β†’