variables in shell scripts

A variable is a variable parameter, a value, a string

  1. When a certain string is frequently used in the script and the string length is very long, a variable should be used instead, which can save the script capacity; when the variable value changes, only one place needs to be changed, and it is easy to modify
  2. When using conditional statements, variables are often used if [ $a -gt 1 ]; then ... ; fi
  3. Substitute a variable for n= when referencing the result of a commandwc -l 1.txt
  4. When writing scripts that interact with users, variables are also essential read -p "Input a number: " n; echo $n If you don't write this n, you can use $REPLY directly
  5. Built-in variables $0, $1, $2… $0 represents the script itself, $1 is the first parameter, $2 is the second.... $# represents the number of parameters
  6. Math operations a=1;b=2; c=$(($a+$b)) or $[$a+$b]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325060147&siteId=291194637