Scope issues of variables defined in shell functions

The scope of a variable defined outside a function starts from where it is defined and ends where the shell ends or is explicitly deleted.
The scope of variables defined within a function starts from where it is called and ends where the shell ends or is deleted explicitly.

A very simple example illustrates the problem:

	#!/bin/bash
	func_(){
    
    
	    var1='123'
	}
	func_
	echo $var1

Output results

123

Therefore, after a function is called, variables defined within the function still exist after leaving the function.
This problem can easily lead to bugs in the program, so please pay more attention.

Guess you like

Origin blog.csdn.net/weixin_43669978/article/details/132540601