linux source bash ./ sh fork exec 的区别

本文详细介绍了在Linux系统中执行Shell脚本的多种方式,包括source、sh、bash及./等命令的区别,并通过实验对比了它们对当前Shell环境的影响。
部署运行你感兴趣的模型镜像

在linux 里做一件事的方法往往有很多种,比如执行一个脚本的就有很多种方式,那么这些方式有什么区别呢?

首先,执行一个shell 脚本的方法有以下几种:source  sh  ./  bash ,那么这些方式有什么区别呢?它们的区别主要是在当前shell环境中运行还是另外开启一个子shell运行以及运行该shell对当前shell环境的影响。

1 source

source 是在当前shell环境下读取该文件的内容,并执行该脚本里的语句,运行结果会影响当前shell的环境,该命令并不会重新启动一个子进程去执行该脚本。

[make@master test]$ echo $$
16575
[make@master test]$ vi s
#!/usr/bin/env bash
echo $$

[make@master test]$ source s
16575
[make@master test]$

由此可见运行shell的进程和当前shell的进程是一样的。

如果修改了 /etc/profile 文件,想让修改立即生效用source命令即可:

source /etc/profile

  source  可以简写成 .


2 sh bash

这两个命令均是linux的命令,意思是开启一个子进程去执行该脚本。因为是子进程,所以该脚本并不会对当前环境产生影响。

3 ./

这个需要该脚本具有可执行的权限,也是开启一个子进程去执行脚本。上面讲述的几个命令均不需要脚本具有可执行权限。


4、fork、source、exec

  • 使用fork方式运行script时, 就是让shell(parent process)产生一个child process去执行该script,当child process结束后,会返回parent process,但parent process的环境是不会因child process的改变而改变的。
  • 使用source方式运行script时, 就是让script在当前process内执行, 而不是产生一个child process来执行。由于所有执行结果均于当前process内完成,若script的环境有所改变, 当然也会改变当前process环境了。
  • 使用exec方式运行script时, 它和source一样,也是让script在当前process内执行,但是process内的原代码剩下部分将被终止。同样,process内的环境随script改变而改变。

通常如果我们执行时,都是默认为fork的。

为了实践下,我们可以先建立2个sh文件,

1.sh

#!/bin/bash
A=B
echo "PID for 1.sh before exec/source/fork:$$"
export A
echo "1.sh: \$A is $A"
case $1 in
    exec)
        echo "using exec..."
        exec ./2.sh ;;
    source)
        echo "using source..."
        . ./2.sh ;;
    *)
        echo "using fork by default..."
        ./2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"

2.sh

#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
export A
echo "2.sh: \$A is $A"


运行下面命令观察结果:

chmod +x 1.sh
chmod +x 2.sh
./1.sh fork
./1.sh source
./1.sh exec

参考:http://www.cnblogs.com/pcat/p/5467188.html




您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

tp@ubuntu:~/Project/Iford_IMD00V5.1.1/Iford/SourceCode/kernel$ grep -r 'CONFIG_STACKPROTECTOR' . ./kernel/panic.c:#ifdef CONFIG_STACKPROTECTOR ./kernel/configs/android-recommended.config:CONFIG_STACKPROTECTOR_STRONG=y ./kernel/fork.c:#ifdef CONFIG_STACKPROTECTOR ./.config.old:# CONFIG_STACKPROTECTOR is not set ./.config.old:# CONFIG_STACKPROTECTOR_STRONG is not set ./Makefile:stackp-flags-$(CONFIG_STACKPROTECTOR) := -fstack-protector ./Makefile:stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG) := -fstack-protector-strong ./.config:CONFIG_STACKPROTECTOR=y ./.config:CONFIG_STACKPROTECTOR_STRONG=y ./include/generated/autoconf.h:#define CONFIG_STACKPROTECTOR_STRONG 1 ./include/generated/autoconf.h:#define CONFIG_STACKPROTECTOR 1 ./include/linux/sched.h:#ifdef CONFIG_STACKPROTECTOR ./include/linux/stackprotector.h:#if defined(CONFIG_STACKPROTECTOR) || defined(CONFIG_ARM64_PTR_AUTH) ./arch/csky/kernel/process.c:#ifdef CONFIG_STACKPROTECTOR ./arch/arm64/Kconfig: support pointer authentication, then CONFIG_STACKPROTECTOR can be ./arch/arm64/kernel/asm-offsets.c:#ifdef CONFIG_STACKPROTECTOR ./arch/arm64/kernel/process.c:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) ./arch/arm64/Makefile:ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y) ./arch/arm64/include/asm/stackprotector.h:#if defined(CONFIG_STACKPROTECTOR) ./arch/arm64/include/asm/stackprotector.h: if (!IS_ENABLED(CONFIG_STACKPROTECTOR_PER_TASK)) ./arch/xtensa/kernel/entry.S:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/xtensa/kernel/asm-offsets.c:#ifdef CONFIG_STACKPROTECTOR ./arch/xtensa/kernel/process.c:#ifdef CONFIG_STACKPROTECTOR ./arch/mips/kernel/r2300_switch.S:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/mips/kernel/octeon_switch.S:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/mips/kernel/asm-offsets.c:#if defined(CONFIG_STACKPROTECTOR) ./arch/mips/kernel/process.c:#ifdef CONFIG_STACKPROTECTOR ./arch/mips/kernel/r4k_switch.S:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/mips/configs/rs90_defconfig:# CONFIG_STACKPROTECTOR is not set ./arch/x86/kernel/cpu/common.c:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/kernel/asm-offsets.c:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/kernel/head_32.S:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/kernel/asm-offsets_64.c:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/kernel/asm-offsets_32.c:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/entry/entry_32.S:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/entry/entry_64.S:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/include/asm/stackprotector.h:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/include/asm/segment.h:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/include/asm/processor.h:#ifdef CONFIG_STACKPROTECTOR ./arch/x86/purgatory/Makefile:ifdef CONFIG_STACKPROTECTOR ./arch/x86/purgatory/Makefile:ifdef CONFIG_STACKPROTECTOR_STRONG ./arch/arm/kernel/entry-armv.S:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/arm/kernel/entry-armv.S:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/arm/kernel/asm-offsets.c:#ifdef CONFIG_STACKPROTECTOR ./arch/arm/kernel/asm-offsets.c:#ifdef CONFIG_STACKPROTECTOR_PER_TASK ./arch/arm/kernel/process.c:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) ./arch/arm/kernel/process.c:#ifdef CONFIG_STACKPROTECTOR_PER_TASK ./arch/arm/configs/iford_ssc029d_s01a_spinand_fastboot_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssz029c_s01b_lh_nor_demo_dualsnr_rt_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_fpga_lh_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssz029c_s01b_spinand_fastboot_demo_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029d_s01a_emmc_fastboot_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029c_s01a_vm_spinand_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029d_s01a_emmc_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029b_s01a_lh_spinand_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssz029c_s01b_lh_spinand_demo_dualsnr_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssz029c_s01b_lh_nor_demo_dualsnr_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029a_s01a_vm_spinand_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029b_s01a_vm_spinand_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029a_s01a_lh_spinand_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssz029c_s01b_fastboot_demo_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssz029c_s01b_lh_nor_demo_dualsnr_usbcam_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029d_s01a_fastboot_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029d_s01a_demo_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029a_s01a_lh_tiny_spinand_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/configs/iford_ssc029b_s01a_lh_nor_defconfig:# CONFIG_STACKPROTECTOR_STRONG is not set ./arch/arm/Makefile:ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y) ./arch/arm/include/asm/thread_info.h:#ifdef CONFIG_STACKPROTECTOR_PER_TASK ./arch/arm/include/asm/stackprotector.h:#ifndef CONFIG_STACKPROTECTOR_PER_TASK ./arch/riscv/kernel/process.c:#ifdef CONFIG_STACKPROTECTOR ./arch/sh/kernel/process_32.c:#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ./arch/sh/kernel/process.c:#ifdef CONFIG_STACKPROTECTOR ./arch/powerpc/kernel/asm-offsets.c:#ifdef CONFIG_STACKPROTECTOR ./arch/powerpc/kernel/entry_64.S:#if defined(CONFIG_STACKPROTECTOR) ./arch/powerpc/Makefile:cflags-$(CONFIG_STACKPROTECTOR) += -mstack-protector-guard=tls ./arch/powerpc/Makefile:cflags-$(CONFIG_STACKPROTECTOR) += -mstack-protector-guard-reg=r13 ./arch/powerpc/Makefile:cflags-$(CONFIG_STACKPROTECTOR) += -mstack-protector-guard-reg=r2 ./arch/powerpc/Makefile:ifdef CONFIG_STACKPROTECTOR ./arch/powerpc/include/asm/paca.h:#ifdef CONFIG_STACKPROTECTOR ./arch/powerpc/xmon/xmon.c:#ifdef CONFIG_STACKPROTECTOR ./Documentation/security/self-protection.rst:return address (``CONFIG_STACKPROTECTOR``), which is verified just before 这个功能是在哪里启用的
10-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值