What are linux-shell- command substitution and variable substitution
This article mainly introduces what linux-shell- command replacement and variable replacement are. It is very detailed and has a certain reference value. Friends who are interested must read it!
Command replacement $() ``Shell command replacement refers to assigning the output of the command to a variable
There are two ways to complete command substitution in Shell, one is backquotation mark, the other is $(), which is used as follows:
Variable= `commands`variable = $(commands)
Where variable is the variable name and commands is the command to be executed. Commands can have only one command or multiple commands separated by semicolons.
A = `expr 1 + 1`echo $a $() differs from ``in operation, both of which achieve the corresponding effect, but it is recommended to use $() for the following reasons: 1. It is easy to mess up, especially for beginners. two。 In multi-level compound substitution,''must require additional escape character processing (backslash), while $() is more intuitive. The downside of $() is that not all unix-like systems support this approach, but backquotes do. Variable replacement ${} variable substitution can change the form of variable substitution in which the value of a variable can be used according to its state (whether it is empty, defined, etc.): form description ${var} the original value of the variable ${var:-word} if the variable var is empty or has been deleted (unset), then word is returned without changing the value of var. ${var:=word} if the variable var is empty or has been deleted (unset), then return word and set the value of var to word. ${var:?message} if the variable var is empty or has been deleted (unset), the message message is sent to the standard error output, which can be used to detect whether the variable var can be assigned normally. If this replacement appears in the Shell script, the script will stop running. ${var:+word} if the variable var is defined, it returns word, but does not change the value of var. The above is all the contents of linux-shell- command replacement and variable replacement. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!