Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement variable replacement Technology under Linux

Shulou Source: shulou.com Published: 2022-06-01 22:37:31 09月20日 Update

This article mainly introduces how to achieve variable replacement technology under Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1.1.1 ${value:-word} returns word if the variable value is not defined, or the value of the value variable if it has been assigned

Result=$ {jimyy:-UNSET}

Echo $result

Because jimyy is not defined, UNSET is returned

This variable function determines that a variable always has a return value when it is not defined.

1.1.2 ${value:=word} when value is not defined, word is assigned to value, and then the vaule value is printed with the ${value:=word} value.

Result=$ {jimyy:=UNSET}

Echo $result

Echo $jimyy

As a result, the values of both variables are UNSET

The function of this variable is to assign a value to the variable when it is not defined and return.

1.1.3 ${value:? "not defined"} when value is not defined, return bash: vaule: not defined

Used to catch errors caused by undefined variables

1.1.4 ${value:+word} returns word when the variable exists and is not null, otherwise null is used to test whether the variable exists

1.1.5 ${value-word} if value does not exist, replace it with word.

The variable substitution function has significant functions in some scenarios.

When operating on a variable, it is best to determine whether the variable is not empty, for example, to perform rm-rf operation on a variable of directory type, which must be judged.

Because if the variable is empty or no value is assigned, it may be deleted from the root directory or / root directory, and the impact is particularly great, so you should be cautious, so you can use the variable substitution feature in this scenario to prevent accidental deletion.

For example:

Path2=/opt/server/backup/

Find ${path2:=/tmp/}-name "* .tar.gz"-type f | xargs rm-f

Thank you for reading this article carefully. I hope the article "how to realize variable replacement Technology under Linux" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Variables functions articles techniques scenarios catalogs significant unexpected below two value interest at the same time in more best friends root directory knowledge type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Huawei Microsoft MySQL