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

How to use parentheses and backquotes in Bash scripts

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use parentheses and backquotes in Bash scripts". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use parentheses and backquotes in Bash scripts.

In Bash scripts, we often need to refer to variables or command execution results as file names or log output. In scripts or Shell, we can use backquotes or parentheses () to get the contents of variables.

Example

Example 1

Get the output of the date

Echo `date` # Tuesday, January 5, output2021 15:21:23 CSTecho $(date) # Tuesday, January 5, output2021, 15:21:32 CST

By using backquotes or parentheses, we can use the output of the command as the content input in the script, and we can also use the parameters of the command to format the output.

Example 2

Get the date in year-month-day format

Echo `date +% F` # output2021-01-05echo $(date +% F) # output2021-01-05 discussion

In Bash, backquotes and parentheses represent command input, so we can only use backquotes and parentheses in the script to get the execution and output of the command, not the variables declared in the script. Using $and ${}, you can get the variables declared in the script instead of the input of the command.

#! / usr/bin/bashvariable=name# use ``to output variableecho `variable` # use $to output variableecho $variable# use $() to output variableecho $(variable) # use ${} to output variableecho ${variable}

The result of the execution reported an error.

▶. / test

. / test: line 6: variable: command not found

Name

. / test: line 12: variable: command not found

Name

Summary

1). (dot) like the source command, the command is read from the file and executed correctly, regardless of whether the file has executable permissions or not. And execute under the current shell instead of generating a sub-shell to execute (we usually use ". / filename.sh" to execute a file that generates a sub-shell to execute under the current shell)

2): this command does nothing but returns a correct exit code, exit 0, similar to pass

3) () execute multiple commands together, which is equivalent to a command group.

4) {} is similar to (), which combines multiple commands together. The difference between them is that () is executed under the resulting child shell, while {} is executed under the current shell.

5) [] like the test command, it is used to compare values and check file types

6) [[]] can be said to be an "enhanced version" of [], which can combine tests supported by multiple test commands.

7) (()) specializes in numerical operations. If the expression evaluates to 0, the exit state is set to 1; if the evaluation is non-zero, it is set to 0.

At this point, I believe you have a deeper understanding of "how to use parentheses and backquotes in Bash scripts". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report