Shell Script—declare

In the shell, declarecommands are used to declare attributes of variables. You can use this command to define attributes such as variable type, scope, readonly, and export.

Here are some common declarecommands to use:

1. Define the variable type

declare -i num  # 声明一个整数变量
declare -a arr  # 声明一个普通数组
declare -A assoc_arr  # 声明一个关联数组

When declaring variables with the declare command

  • Use -ithe attribute to indicate that the variable is an integer type variable
  • Use -athe attribute to indicate that the variable is a variable of ordinary array type
  • Use -Athe attribute to indicate that the variable is an associative array variable

2. Define the scope of variables

declare -g var  # 声明一个全局变量

-gVariables can be declared global using the attribute. If this attribute is not added, the variables that appear in the sub-shell by default will be ignored. You can use the command in the shell export varto export variables as global variables, but declare -gvariables declared with can be used directly in the current shell-session.

Sub-Shell refers to a new Shell process, which is derived from the current Shell process (that is, the parent process). Sub-Shell is more common when executing Shell scripts. It will execute some commands in the child process and return the result to the parent process. Variables defined in the Sub-Shell are only valid within that process, and the parent process cannot access any variables or results in the child process. For example, when using pipes in a shell script, all commands after each pipe character (|) will be executed in a new Sub-Shell process. The output of this process will become the input of the pipe without any effect in the original shell process.

Shell-Session refers to a continuously running Shell environment, which includes all processes and variables currently running Shell. Variables defined in a Shell-Session are visible to all processes in the Shell-Session, so they can be shared and reused during a session. For example, in the Bash Shell, you can use exportthe command in a Shell-Session to export a variable as an environment variable, so that all child processes can access the variable during the Shell session.

Bash-Shell refers to a shell program used on Linux and Unix operating systems. Bash is an extension of the Bourne Shell (sh) in the Unix operating system and is backward compatible with sh. It provides many extended features such as command history, autocompletion, job control, etc. Bash also supports various programming structures, including conditional statements, loop statements and functions, etc., so that Bash scripts can be written as complex as other programming languages.

3. Define read-only variables

declare -r var='hello'  # 声明一个只读变量

Use -rthe attribute to define a read-only variable whose value cannot be modified after assignment.

4. Export variables as environment variables

declare -x var='world'  # 声明一个导出为环境变量的变量

Use -xthe attribute to export variables as environment variables, available in the current shell process and in child processes.

In short, declarecommands can help us define various variable attributes in Shell scripts, thereby improving the flexibility and maintainability of Shell scripts.

Guess you like

Origin blog.csdn.net/shouhu010/article/details/131393019