1. Set variables
Echo ${<variable>} # echo ${<variable>} # echo ${<variable>} #Copy the code
  1. There is one between the variable and the value=The connection
  2. There can’t be Spaces on both sides of the equal sign
  3. The variable name must contain only letters and numbers and cannot start with a number
  4. Variable content can be enclosed with double or single quotation marks if it has Spaces. In general, variable content contains references to variables, that is, contain$<variable>, are wrapped in double quotation marks. Common characters are wrapped in single quotation marks.
  5. You can use the escape character “\” to make a special symbol generic.
  6. If the variable content contains information provided by other commands, use the following two forms:

    • ` \ `

    • $(<command>)

  7. To add content to a variable, you can use $(

    )addition or $

    addition. Such as:

    8. The 'export' keyword is used to set environment variables that can be passed to the current bash and child processes. 9. The uppercase letters are the system default variables, and the lowercase letters are the variables you set. For example, 'PATH,HOME,SHELL' are system variables. Here is a simple example to verify the above syntax: Shell TMP ="I am TMP "#* Echo "second type ${TMP}" #*"${<variable>}" echo 'third type ${TMP}' #*'$<variable>' echo "with acutual '$' is \$TMP "#* Echo "with acutual '$' is \$TMP" #* Echo "with acutual '$' is \$TMP "#* Echo "with acutual '$' is \$TMP" #* Echo "with acutual '$' is \$TMP "#* Echo "with acutual '$' is \$TMP" #* Echo "with acutual '$' is \$TMP Echo "first $version" version= 'PWD' #* 'command' echo "second $version" unset version #* TMP ="${TMP} add new words" # Echo $TMP #* var="parent process var" export var #* echo "parent pid=?"Copy the code

The comments are well explained. Look at the output:

First type I am TMP second type I am TMP third type ${TMP} with acutual '$' is $TMP first 17.4.0 second / Users/saka/Downloads/Markdown/shell commands no version I am TMP add new words parent pid = 19895Copy the code
2. Scope of variables

The scope of variables is divided into environment variables, global variables and local variables.

  • Environment variables are defined variables in the systemenvYou can:
SHELL=/bin/zsh TMPDIR=/var/folders/1x/gcpxck2s0jxbfs6qll8p0jlh0000gn/T/ Apple_PubSub_Socket_Render = / private/TMP/com. Apple. Launchd is that it. X4uBMH5DE9 / Render TERM_PROGRAM_VERSION = 1.20.0 ZSH=/Users/saka/.oh-my-zsh USER=saka COMMAND_MODE=unix2003 PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin The PWD = / Users/saka/Downloads/Markdown/shell command LANG = zh_CN. Utf-8 XPC_FLAGS = 0 x0 XPC_SERVICE_NAME = 0 var = parent process var VSCODE_NODE_CACHED_DATA_DIR_18562=/Users/saka/Library/Application Support/Code/CachedData/c63189deaa8e620f650cc28792b8f5f3363f2c5b SHLVL=30 HOME=/Users/saka VSCODE_NLS_CONFIG={"locale":"zh-cn","availableLanguages":{"*":"zh-cn"}} LESS=-RCopy the code

These variables can be retrieved directly from shell scripts.

  • Global variables Variables defined in shell scripts, including functions in shell scripts, are global and are scoped from where they are defined to where the shell ends or is displayed to be deleted.
  • Variables defined by a function can be explicitly defined as local, whose scope is limited to the function. Note, however, that the arguments to the function are local. The local variable defined by the Shell function will mask the global variable defined by the script if the variable has the same name.

A simple example:

Echo "env variable SHELL=${SHELL}" #* echo "env variable SHELL=${SHELL}" Echo "function SHELL=$SHELL" echo "function SHELL=$SHELL" # Echo "local var=${var}" #* Echo "after showshell SHELL=${SHELL}" #* Echo "local var=${var}" #* echo "local var=${var}" #* echo "after unset SHELL=${SHELL}" #*Copy the code

View the output:

env variable SHELL=/bin/zsh
function SHELL=custom shell
local var=local var
after showshell SHELL=custom shell
local var=
after unset SHELL=
Copy the code
3. Operations on variables

Let’s start with a summary:

  • Substitution and deletion of variables
Variable setting mode instructions
${variable # keyword} If the data from the beginning of the variable content matches the keyword, the shortest data matching the keyword will be deleted
$(variable ## keyword) If the data from the beginning of the variable content matches the keyword, the longest data matching the keyword is deleted
${variable % keyword} If the data from the end of the variable content forward matches the keyword, the shortest data matching the keyword will be deleted
${variable %% keyword} If the data from the end of the variable content forward matches the keyword, the longest data matching the keyword will be deleted
${variable/old string/new string} If the variable content matches “old string”, the first old string is replaced by the new string
${variable // old string/new string} If the variable content matches “old string”, all old strings are replaced with new strings
Path ="abcdeaAbcedeA" echo "character * Matches any character, Echo ${path#a*de} echo ${path#a*de} echo ${path#a*de} echo ${path#a*de} echo ${path#a*de} echo ${path#a*de} echo Echo ${path% a* c} echo "echo ${path% a* c} echo" echo ${path% a* c} echo "echo ${path% a* c} echo" echo ${path% a* c} echo "echo ${path% a* c} echo" echo ${path% a* c} echo" Echo ${path/ A /Z} echo ${path/ A /Z} echo ${path/ A /Z} echo ${path/ A /Z} echo ${path/ A /Z} echo ${path// A /Z} echo ${path// A /Z} echo ${path// A /Z} echo ${path// A /Z} echo ${path// A /Z} echo ${path// A /Z} echo ${path// A /Z} echo ${path// A /Z}Copy the code

Output result:

The character * matches any character, So here's the old value abcdeaAbcedeA matches the first 'A' from the beginning, and then deletes it all the way to the first 'de' that's encountered. AAbcedeA matches the first 'a' from the beginning, deletes it all the way to the last 'de' a' from the end, Then delete until the first 'C' abcdeaAbcedeA matches the first 'A' from the end, Then delete until you find the last 'c' abcdeaAbcedeA and replace one A with Z ZbcdeaAbcedeA and replace all a's with Z ZbcdeZAbcedeACopy the code
  • Variable Settings
Set mode STR not set STR = “” STR has value
var=${str-expr} var=expr var= var=$str
var=${str:-expr} var=expr var=expr var=$str
var=${str+expr} var= var=expr var=expr
var=${str:+expr} var= var= var=expr
var=${str=expr} str=expr

var=expr
STR unchanged

var=
STR unchanged

var=$str
var=${str:=expr} str=expr

var=expr
str=expr

var=expr
STR unchanged

var=$str
var=${str? expr} Expr outputs to stderr var= var=str
var=${str:? expr} Expr outputs to stderr Expr outputs to stderr var=str

This one is easier, don’t write test cases. A brief explanation of “and” : :

If the preceding variable is not set, the following variable is taken. If the preceding variable is set, the preceding variable is printed. If :- is used together, if the preceding variable is not set or its value is “”, the preceding value is replaced by the following value, otherwise the preceding value is not replaced

4. Declaration of variables
  • Set the value of a variable by user input

Declaration form

Read [-pt] <variable> -p: the prompt is followed by -t: the waiting time, in secondsCopy the code

Write a simple script test:

#/bin/zsh echo "Read -p "var1= "var1 read -p "var2= "var2 read -p" You have entered all variable values. y/n:" print if [ ${print}x == yx ]; then echo "var1= $var1" echo "var2= $var2" elif [ ${print}x == nx ]; then echo "complete" else echo unknow command fiCopy the code

Execute:

Please follow the instructions below to enter the value you want to set var1= 9 var2= 8 You have entered all the variable values, do you want to print it now? y/n:y var1= 9 var2= 8Copy the code
  • declare/typeset

Define the form

Declare [-aixrp] Variable parameter: -a: defines a variable as an array type -i: defines a variable as an integer. -x: outputs a variable as a ring variable. -r: sets a variable as a readonly type, which is equivalent to a Constant in JavaCopy the code

Declare -x

can promote a variable to an environment variable, and DECLARE +x

can cancel a variable to an environment variable.

Variables in bash are strings by default if they are not declared. Simple operations can be performed after the declaration. A simple example:

#bin/bash sum_one=100+10 It is a string of characters echo $SUM_ONE #* 100+10 DECLARE -I sum #* Declared as an integer sum=100+10 #* Calculated result Echo $sum #* 110 DECLARE -A array #* Defined array Array [1] = "a" # * define the elements 1 array [2] = "b" # * define the elements 2 echo ${array [*]} # * the output arrayCopy the code
100+10
110
a b
Copy the code