在 shell脚本里面执行执行其他 shell脚本
时间: 2024-08-13 09:02:18 浏览: 152
在Shell脚本中执行另一个Shell脚本是很常见的操作,你可以使用命令`source`(在bash和其他一些shell中)、`.`(点号)或者`./`(当前目录下执行)来完成。以下是几种常见的情况:
1. `source script.sh`: 这种方式会逐行读取并执行指定脚本(`script.sh`),改变了当前 shell 的环境变量。这种方式保留了原脚本的上下文。
2. `. script.sh`: 同样是逐行执行另一个脚本,语法上等价于`source`。
3. `./script.sh`: 如果你想直接运行另一个文件,通常假设它位于当前目录下,使用这个命令。这不会改变当前shell环境,而是创建一个新的子进程去执行脚本。
例如,在`main.sh`脚本中,你可以这样调用另一个脚本:
```sh
#!/bin/bash
# 执行 other_script.sh
./other_script.sh
```
如果你想在`other_script.sh`中修改环境变量,你应该在`other_script.sh`开头加上`export`或`set -x`(显示详细命令行)来让更改生效。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![jpg](https://img-home.csdnimg.cn/images/20210720090814.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)