Shell Programming: Typeable variables

1. Through the declare and typeset commands

  • declare and typeset are equivalent
  • declare and typeset are both used to define variable types

    li>

  • The following is a summary with declare

2, declare parameters are as follows

  • -r Set the variable to read-only
  • -i Set the variable to an integer
  • -a Set the variable to an array
  • -F Display all functions and content defined before this script
  • – f Only display the function names defined before this script
  • -x Declare variables as environment variables

3. Example: p>

Example 1: Declare as immutable, declare -r

  $ var=hello

  $ var=world

  $ echo $var

   print out: world // Explain that var is variable

  $ declare -r var=hello

  $ var=world  

  Print result: -bash: var: read-only variable

Example 2: Declare as an integer, declare -i

  $ num=10

  $ num2=$num+20

  $ echo $num2

   print out: 10+20

$ expr $num + 20

   prints out: 30

  

  $ num=10

  $ declare -i num3

  $ num3=$num+90

  $ echo $num3

   print out: 100

Example 3: Display the defined in the system< strong> Function name and function body

  $ declare -F

Example 4: Display the function names of all functions defined in the system

$ declare -f

Example 5: Declare as an array, declare -a

  $ declare -a array // Declare an array

  $ array=(“jones “”mike” “kobe” “jordan”) // Assign value

   Output array content:

    $ echo ${array[@]}    // Output all content, print out: jones mike kobe jordan

     $ echo ${array[0]}  // Output the value of the array element subscript as 0, and print out: jones

  Get the length of the array:

    $ echo ${#array[@]} // Output the length of the array, print out: 4

    $ echo ${#array[0]} ​ // The 0th element of the output array Length, print out: 5

   Assign a value to a subscript of the array:

    $ array[0]=lisi

    $ echo ${array[@] } // Output: lisi mike kobe jordan

   Add an element to the end of the array:

    $ array[10]=zhangsan // The subscript must be greater than the length of the array-1, which can be larger Many

   delete elements:

    $ unset array[2] // Delete the third element of the array

    $ unset array /// Clear the entire array

p>

   Fragmented access:

    $ echo ${array[@]:1:3} // Starting from the position where the subscript is 1, take 3 elements backward, ignoring the middle Empty elements until enough 3 elements are fetched. If there are less than 3 elements, just output all the following elements.

   content replacement:

    $ array2=${array[@]/ke/KE} // Replace all ke in the array with KE.

    $ echo ${array2[@]}

  Array traversal:

    for v in ${array[@]}

do

      echo $v

    done 

Example 6: Declare as an environment variable declare -x     

   

num After num becomes an environment variable, it can be used directly in the script. If it is not an environment variable, it can only be used in this terminal.

4. Undeclared variables

  declare +r

  declare +i

  declare +a

  declare +x

Summary: To the array array [0,1,2, 3,4] Add an element at the end. If the length of the array is 5 and the subscript points to 7 when adding a new element 7, then the array is [0,1,2,3,4,, ,7], the length of the array at this time Is 6, at this time, the values ​​of array[5] and array[6] are empty, and the value of array[7] is 7.

Leave a Comment

Your email address will not be published.