Linux bash Shell's declare
declare or typeset built-in commands (which are exactly the same) can be used to constrain the properties of a variable. This is a way to use a less restrictive definition type in some programming languages. Command declare is only available after Bash version 2. The command typeset can also be run in the Ksh script .
Declare/typeset Options
- R Read-only
#!/bin/bashdeclare-r Haha=leafecho $hahahaha =whatecho What's $haha?
The results of the operation are as follows and cannot be modified until the declaration is visible.
- [Email protected] mnt]#./declare2.sh
Leaf
./declare2.sh:line 11:haha:readonly Variable
What's the leaf?
-I shaping
#!/bin/bashdeclare-i numnum=1echo num= "$num" Num=2echo num= "$num" Num=threeecho num= "$num" num=1num= $num +1echo num= "$ Num
[Email protected] mnt]#./declare3.sh
Num=1
num=2
Num=0
num=2
#!/bin/bashn=10/5echo n= $ntypeset-i n//and declare an effect echo n= $n//Note here the output N=10/5echo n= $n
[Email protected] mnt]#./declare4.sh
#!/bin/bashfoo () {Declare foo= "bar" echo "Now foo= $FOO" return 20}bar () {fooif [$?-eq];thenecho "$FOO" Else echo nonof i} Bar
[Email protected] mnt]#./declare1.sh
Now Foo=bar
As you can see, declare declares a local variable, so the function call is gone.
-F Displays all previous functions followed by the function name specified to display a function
7 #!/bin/bash 8 Foo () 9 { 10 declare foo= "bar" 11 echo "now foo= $FOO" 12 return 20 13 } 14 bar () 15 { 16 foo 17 if [ $? -eq 20 ];then 18 echo "$FOO" 19 else 20 echo nono 21 fi 22 } 23 bar 24 declare -f 25 declare -f foo
[Email protected] mnt]#./declare1.sh
Now Foo=bar
Bar ()
{
Foo
If [$?-eq 20]; Then
echo "$FOO";
Else
Echo Nono;
Fi
}
Foo ()
{
DECLARE foo= "Bar";
echo "Now foo= $FOO";
return 20
}// declare-f results
Foo ()
{
DECLARE foo= "Bar";
echo "Now foo= $FOO";
return 20
} //declare-f foo results
Shell script declare declaring variables