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

Example Analysis of Command substitution in Shell

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you the "sample analysis of command substitution in Shell", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of command substitution in Shell".

What is command replacement?

To put it simply, multiple commands are nested in SHELL and executed at once to get the result.

1. SHELL nesting # echo `whoami` # echo $(whoami) # echo "hello, `whoami`" # echo "hello,$ (whoami)"

2. Layer 2 SHELL nesting # echo `cat. / gn2.txt` | sed-s "s politics;-- list;"

Use ``to read the contents of the file, and then use the pipe character for secondary processing. Execute!

Note: one-layer nesting has been used here, and the following sections will be applied to achieve two-layer nesting.

a. Use "$()" for two-layer nesting

b. Use "|" for command orientation

c. Notes and answers to some questions

Some readers may have noticed that there is another kind of nesting in simple SHELL nesting. Then why not use ``for nesting.

The root cause is: ``does not support nested execution of commands!

Enforcement. Only the * * group can be recognized, and the rest will be executed individually by spaces or by pipe characters (inclusive) until the end.

It's old. Easily confused with "single quotation marks".

It is the backquotation mark key containing ~ under the ESC in the upper left corner of the American keyboard!

There are substitutes. $(.) The format is supported by the POSIX standard and facilitates nesting.

$() can be nested in multiple layers like $()), but it can also be executed if there is an internal ``(for compatibility reasons)!

3. Advance

Earlier we introduced ``and $(.), these two kinds of command execution.

I think you must have a deep understanding of command execution by now. Now, we need to take it to the next level.

1. (cmd) and {var}

Both () and {} are related concepts under the parent class of the shell extension as well as command substitution.

Tip: {} there must be a space to the right of the head braces and a semicolon to the left of the trailing braces.

# (echo firest;echo second;) # {echo third;echo fourth;}

Note: () just re-open a sub-shell for a series of commands and execute {} on the current shell for a series of commands.

2. The influence of () and {}

A. () the statement in parentheses is affected in parentheses

# var=source# (echo $var;var=global;echo $var;) # echo $var

B. {} statements in parentheses affect the overall situation.

# echo $var# {echo $var;var=global;echo $var;} # echo $var

Note: {} after changing the variable of var, the external is also affected.

What is parameter extension

The basic format of the parameter extension is ${parameter}, and the result of the extension is that ${parameter} is replaced with the corresponding value.

1. Example-echo $1 $11echo $1 ${11}

First of all, explain what ${1. 9} means. When we write Shell, we inevitably need to pass parameters to implement custom variables. When it exceeds the Arabic numeral 9. You need to use ${parameter} to explicitly tell Shell that the 11th parameter is ${11}.

Tip: the figure above shows 101 because $11 does not meet [1-9] {1}. The system splits 11 into $1 and 1, so the result is 101.

2. Example 2: ban = banecho a $bananaecho a ${ban} ana

In this example, I want to output banana. A variable of ban has been defined as ban, which can be "stupid" as long as you add ana.

But obviously it is impossible to make the variable $ban display banana with ana without adding {}!

What is variable extension

By official definition, I should not have created a "variable extension" out of nothing.

"the $character introduces parameter extensions, command substitution, or arithmetic extensions." -- official manual

It is mainly out of two considerations:

Most accept it. There are a large number of articles on "variable expansion" in China, and most people have accepted the name.

It's easy to understand. The argument is ${.} something in parentheses, and the word variable means that all operations are expanded around the variable.

Easy to record. After syncopation, it is helpful to write the typesetting of this article. It can also be divided from basic, intermediate and advanced levels.

Example:

Var='This is one test sentence.'var1=parametervar2=word

Now that we have a sentence like this, I want to make some judgments, excerpts (or: slices) or modifications. What should I do?

1. Variable replacement

A. ${parameter:-word}

# echo ${var1:-$var2} parameter# var1=# echo ${var1:-$var2} word

If var1 is not set or empty, replace it with var2.

B. ${parameter:=word}

Ditto. Location parameters and special parameters cannot be assigned in this way.

C. ${parameter:?word}

# var1=# echo ${var1:?var2} bash: var1: var2# echo $? 1

When the variable var1 is not set or empty and shell is interactive, an error is reported and exited. If shell is not interoperable, a variable substitution occurs.

D. ${parameter:+word}

# echo $var1parameter# echo $var2word# echo ${var1:+$var2} word# echo $var1parameter

If the var1 is empty or not set, then nothing is done. Otherwise, replace it with var2.

Tip: when I tested it, I found that it didn't work globally.

2. Variable slice

a. Range slice (same direction)

# echo ${var:8:17} one test sentence

Note: both numbers are counted from the beginning.

b. Range slice (non-co-directional)

# echo ${var:8:-1} one test sentence# echo ${var:8: (- 1)} one test sentence

Tip: both writing methods are correct.

c. Slice position

# a='This is one'# echo ${# a} 11

Tip: first create the variable a='This is one', and then use echo ${# a} to read out the number of characters.

3. Variable modification

a. Simple modification

# echo ${var} This is one test sentence.# echo ${var/one/a} This is a test sentence.

Tip: personally, I think this is a * way, which can be modified in a range (including deletion).

b. Simple deletion

# echo ${var%sentence.} This is one test# echo ${var#This is} one test sentence.

c. Attached: form

< 如显示不全,请左右滑动 >

Variable setting method explains ${variable # keyword} if the data starting from the beginning of the variable content conforms to the "keyword", the shortest data will be deleted ${variable # # keyword} if the data starting from the beginning of the variable content conforms to the "keyword", the longest data will be deleted ${variable% keyword} if the data starting from the end of the variable content meets the keyword Delete the shortest data of the match ${variable%% keyword} if the data starting from the end of the variable meets the keyword, delete the longest data of the match ${variable / old string / new string} if the content of the variable matches the "old string", then the old character will be replaced by the new character. ${variable / old string / / new string} if the content of the variable matches the "old string", all the old characters will be replaced by the new characters. The above is all the contents of the article "sample Analysis of Command substitution in Shell". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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: 0

*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