Get the App
SLTechnology News&Howtos  ›  Servers  › 

Declare the shell variable using the declare command of linux

Shulou Source: shulou.com Published: 2022-06-02 15:24:17 09月29日 Update

Do not know how to use linux's declare command to declare shell variables? In fact, it is not difficult to solve this problem. Let the editor take you to learn how to solve it. I hope you will gain a lot after reading this article.

The declare command is used to define shell variables, but variables defined by declare can only work in the current shell environment, and will have no effect after switching the shell environment.

Syntax format: declare [parameters] [directory]

Common parameters:

-a declares array variables-f displays only functions-F does not display function definitions-I first evaluates the expression and assigns the result to the declared variable-p the method and value that displays the definition of a given variable, and when using this option, other options are ignored-r defines a read-only variable-x converts the specified Shell variable to an environment variable

Reference example

Display defined variables:

[root@linuxcool ~] # declare

Define a new Shell variable:

[root@linuxcool ~] # declare var1= "hello"

Use "- I" to define a new variable:

[root@linuxcool] # declare-I var2=100+200

Use "- p" to display the definitions of the variables var1 and var2:

[root@linuxcool] # declare-p var1 var2

Show all environment variables:

[root@linuxcool] # declare-x Thank you for reading this article carefully. I hope it is helpful for everyone to share the contents of declaring shell variables using linux's declare command. At the same time, I also hope you can support us, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you!

Tags: Variables environments commands functions parameters methods articles questions learning functions content at the same time examples commonly used arrays formats directories results ribbons industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Shulou Information Linux MariaDB