Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use parentheses and backquotes in Bash scripts

Shulou Source: shulou.com Published: 2022-06-03 02:39:28 10月04日 Update

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!

Tags: Commands scripts quotes files outputs contents variables parentheses are in parentheses multiple examples combinations input size date week format results learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL macOS Shulou Information Shulou Tech Info