Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 从脚本设置变量_Bash_Shell_Var - Fatal编程技术网

Bash 从脚本设置变量

Bash 从脚本设置变量,bash,shell,var,Bash,Shell,Var,我有一个脚本“set_var.sh”是这样写的 #!/bin/bash export NAME=release export ROOT=/Volumes/name/dev/release 但如果我从终端运行这个set_var.sh,之后我会发出set命令来检查变量,我找不到要设置的名称和根变量 我想知道我的情况有什么问题。它是在sub-shell中设置的 你需要 source set_var.sh 如果只运行set_var.sh,它将在自己的shell中运行,该shell将退出,并丢失已设

我有一个脚本“set_var.sh”是这样写的

#!/bin/bash

export NAME=release
export ROOT=/Volumes/name/dev/release
但如果我从终端运行这个set_var.sh,之后我会发出set命令来检查变量,我找不到要设置的名称和根变量


我想知道我的情况有什么问题。

它是在sub-shell中设置的

你需要

source set_var.sh
如果只运行set_var.sh,它将在自己的shell中运行,该shell将退出,并丢失已设置的变量

如果要更改交互式shell中的变量,可以使用:

source set_var.sh
或者速记

. set_var.sh
这将执行脚本的行,就像它们被键入到交互式shell中一样

请注意,当您以这种方式“源”文件时,它不需要第一行的“shebang”


还请注意,Bourne shell中也存在此功能,但仅在简短版本中存在。

运行此功能时,所有变量都会在执行范围内定义。如果希望这些变量保留在会话中,则必须改为
source