A semantics for Bash scripts?

后端 未结 3 677
悲&欢浪女
悲&欢浪女 2020-12-07 07:56

More than any other language I know, I\'ve \"learned\" Bash by Googling every time I need some little thing. Consequently, I can patchwork together little scripts that appea

3条回答
  •  半阙折子戏
    2020-12-07 08:17

    The answer to your question "What is the typing discipline, e.g. is everything a string" Bash variables are character strings. But, Bash permits arithmetic operations and comparisons on variables when variables are integers. The exception to rule Bash variables are character strings is when said variables are typeset or declared otherwise

    $ A=10/2
    $ echo "A = $A"           # Variable A acting like a String.
    A = 10/2
    
    $ B=1
    $ let B="$B+1"            # Let is internal to bash.
    $ echo "B = $B"           # One is added to B was Behaving as an integer.
    B = 2
    
    $ A=1024                  # A Defaults to string
    $ B=${A/24/STRING01}      # Substitute "24"  with "STRING01".
    $ echo "B = $B"           # $B STRING is a string
    B = 10STRING01
    
    $ B=${A/24/STRING01}      # Substitute "24"  with "STRING01".
    $ declare -i B
    $ echo "B = $B"           # Declaring a variable with non-integers in it doesn't change the contents.
    B = 10STRING01
    
    $ B=${B/STRING01/24}      # Substitute "STRING01"  with "24".
    $ echo "B = $B"
    B = 1024
    
    $ declare -i B=10/2       # Declare B and assigning it an integer value
    $ echo "B = $B"           # Variable B behaving as an Integer
    B = 5
    

    Declare option meanings:

    • -a Variable is an array.
    • -f Use function names only.
    • -i The variable is to be treated as an integer; arithmetic evaluation is performed when the variable is assigned a value.
    • -p Display the attributes and values of each variable. When -p is used, additional options are ignored.
    • -r Make variables read-only. These variables cannot then be assigned values by subsequent assignment statements, nor can they be unset.
    • -t Give each variable the trace attribute.
    • -x Mark each variable for export to subsequent commands via the environment.

提交回复
热议问题