Introduction and command of variable scope in shell



It is well known that variables in a shell only work on the current process. To create a copy in a child process, use the export built-in command. Sometimes it’s convenient to use temporary variable syntax.

Variable usage

  • Shell variables can be defined anywhere, using = to separate variable names and variable values. =There can be no spaces before and after, but the value of the variable can be left blank.
  • The $prefix needs to be added to read variables.
  • The variable scope is the current process.

For example:

echo $url

Export to child process

Usually, variables do not need to be exported, but when you need to divide a work into small tasks and use a script to implement them, you need to pass the variable names to them. For example, there is a to download any URL to the temporary directory:

#!/usr/bin/env bash
curl $url > $TMPDIR/$RANDOM.html

We need to pass the URL in the current script to

export url=
bash ./
#Equivalent to (if the file has executable rights)

It is worth noting that export only creates a copy of the variable in the child process, that is, the changes made to it by will not be reflected in the current process.

Execute script in current process

Use the source or. Built-in command to execute another script in the current process, so variables in the current context are visible to the script.


source ./
Equivalent to
. ./

Setting environment variables temporarily

According to shell syntax, any assignment statement can be included before a simple command. These variable assignments are expanded before the command is executed, equivalent to temporary environment variables.

A “simple command” is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator. – Simple Commands, Shell Commands

For example, the following command can pass the URL variable to

url= bash ./

This is a simple command. The following multiple commands or combined commands:

Url =; bash. / ා two commands, only for the current process
Url = & & bash. / ා combined command, only for the current process
Export url =; bash. / ා two commands, which act on the parent-child process

Variable assignments in simple commands do not affect the current process either. For example, the following code will output a blank line:

url= echo $url


The above is the whole content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message and communicate with us. Thank you for your support for developpaer.

Recommended Today

Notes on tensorflow 2 deep learning (I) tensorflow Foundation

This series of notes records the process of learning tensorflow2, mainly based on Learning First of all, it needs to be clear that tensorflow is a scientific computing library for deep learning algorithm, and the internal data is stored in theTensor objectAll operations (OPS) are also based on tensor objects. data type Fundamentals in […]