Shell script declare declaring variables

Source: Internet
Author: User

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

      • N=10/5
        N=10/5//Note the output here, you can see the previous declared variables or the variable, declare need to declare again.
        n=2


  • #!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.