Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are linux-shell- command substitution and variable substitution

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 284

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report