Introduction and command of variable scope in shell

Time:2020-1-18

Preface

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:


url=http://harttle.com
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 spider.sh 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 spider.sh:

export url=http://harttle.com
bash ./spider.sh
#Equivalent to (if the file has executable rights)
./spider.sh

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 spider.sh 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.

url=http://harttle.com

source ./spider.sh
Equivalent to
. ./spider.sh

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 spider.sh:


url=http://harttle.com bash ./spider.sh

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

Url = http://hartle.com; bash. / spider.sh ා two commands, only for the current process
Url = http://hartle.com & & bash. / spider.sh ා combined command, only for the current process
Export url = http://hartle.com; bash. / spider.sh ා 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=http://harttle.com echo $url

summary

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 https://github.com/dragen1860/Deep-Learning-with-TensorFlow-book 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 […]