Variable
System variables
  • Created and maintained by Linux itself, this type of variable defined in CAPITAL LETTERS
  • set, check system variables
  • #!/bin/bash
    
    echo $BASH
    echo $BASH_VERSION
    echo $COLUMNS
    echo $HOME
    echo $PATH
    echo '$USER is: '"$USER"
    			
    User defined variables
  • Created and maintained by user, this type of variable defined in lower letters
  • Don't put spaces on either side of the equal sign when assigning value to variable
  • #!/bin/bash
    
    name='Lin Chen'
    echo $name
    val=bus
    echo $val
    vech= # define NULL variable
    echo $vech
    unset val # unset variable
    			
    Special variables
    #!/bin/bash
    
    echo $$ # process id
    echo $? # The exit status of the last command executed
    echo $! # The process number of the last background command
    			
    Variable Substitution
    #!/bin/bash
    
    # If var is null or unset, print the default value
    echo ${var:-"Not defined ..."}
    
    # If var is null or unset, var is set to the specified value
    echo ${var:="Set value ..."}
    echo $var
    
    # If var is null or unset, message is printed to standard error
    unset var
    echo ${var:?"Warning message ..."} # print warning and exit
    
    # If var is set, word is substituted for var
    var="Hello"
    echo ${var:+"Substitution ..."} # Substitution
    echo $var # Hello 
    			
    Reference
  • Tutoriaspoint