source和sh执行脚本时的差异

在CentOS7下,有如下脚:sh02.sh。

1 用sh或者bash执行

 先执行echo $firstname $lastname

 再执行 sh sh02.sh

 最后执行 echo $firstname $lastname

2 用source执行

先执行  echo $firstname $lastname

再执行 source sh02.sh

最后执行  echo $firstname $lastname

结论:使用sh 执行shell文件时,是在父进程的bash下新建了一个子进程,sh02.sh是在子进程的运行的,执行结束后bash内容自动清空;

   而使用source执行时,是直接在父进程中执行的,没有新建子进程。

时间: 2024-08-27 19:08:33

source和sh执行脚本时的差异的相关文章

【MYSQL】在脚本中使用变量-执行脚本时传参

在shell脚本里可以定义变量,并在执行脚本时任意传参. #!/bin/bashdb_name=$1 #将第一个值赋给db_name变量sql_name=$2 #将第二个值赋给sql_name变量mysql -uroot -p123456 -h192.168.227.129 $db_name < $sql_name 在执行shell脚本时,在执行命令后面直接传参即可 ./sqlrecovery jinqy jinqy.sql

安卓8.0真机运行appium1.4遇到的问题:运行自动化脚本,手机自动安装 settings.apk和unclock.apk,执行脚本时提示安装UnicodeIME-debug.apk失败,怎么关掉自动安装?

运行自动化脚本,手机自动安装 settings.apk和unclock.apk,执行脚本时提示安装UnicodeIME-debug.apk失败,怎么关掉自动安装? 这3个apk的目录分别是: D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk D:\Program Files (x86)\Appium\node_modules\appium\build\unlock_apk D:\Program Files (

curl方式执行脚本时传参问题

参考: https://stackoverflow.com/questions/4642915/passing-parameters-to-bash-when-executing-a-script-fetched-by-curl/4642975 通常执行发布机上的脚本时习惯使用以下方式: curl http://example.com/script.sh | bash 若涉及到传入参数时,则可使用 1. curl http://example.com/script.sh | bash -s ar

./yy.sh -d bash 执行脚本时所加的参数

-e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L filename 如果 filename为符号链接,则为真 -r filename 如果 filename可读,则为真 -w filename 如果 filename可写,则为真 -x filename 如果 filename可执行,则为真 -s filename 如果文件长度不为0,则为真 -h fil

TestComplete中执行脚本时控件值未同步刷新问题

今天写了一个程序安装的自动化脚本,调试的时候发现有些控件的值获取后一直未自动刷新,鉴于该问题,仔细分析和请教后,发现TestComplete不会自动刷新控件中变化的值,这需要我们自己去控制刷新. 1.定义一个延迟函数(参数:延迟秒数,执行延迟的条件) Function TimeDelay(DelaySeconds,condition)    Dim interval,startTime    startTime=Time    Do While CBool(condition)        s

小记---------手动执行脚本正常执行,使用crontab定时执行时 不执行

可能出现的原因就是因为crontab不会从用户的/etc/profile文件中读取环境变量,所以就出现 使用定时crontab执行时 无法执行 抛错 所以在使用crontab 定时执行脚本时  在脚本中尽量使用绝对路径,除去全局变量以外 解决方法: 1. 直接把/etc/profile文件中配置的spark的环境变量写到脚本的开头,使它在执行脚本之前先加载spark的环境变量即可 2.直接在脚本中添加即可 #!/bin/bash #此处添加如下 ./etc/profile num_executo

ssh连接远程主机执行脚本的环境变量问题

用ssh命令ssh [email protected] "/web/tomcat-7000/bin/startup.sh" 登陆到远程机器remote上执行脚本时,遇到一个奇怪的问题:tomcat服务不能启动 Neither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these environment variable is needed to run this progr

执行脚本出现以下问题怎么办?

有时在linux上执行脚本时出现以下错误: bash: ./squid.sh: /bin/bash^M: bad interpreter: 没有那个文件或目录 那么出现这种情况就可能是因为你在windows上编写的脚本,由于windows与linux不完全兼容,所以文件格式可能不同. 解决方法: vim squid.sh :set ff? 如果出现fileformat=dos说明就是文件格式的问题 接着执行:set fileformat=unix :wq 转化以下文件格式就可以接着执行脚本了.

在Powershell中禁止执行脚本

在Powershell中执行脚本时,有如下报错: 无法加载文件 F:\script\1.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 "get-help about_signing". 所在位置 行:1 字符: 8 + .\1.ps1 <<<< + CategoryInfo          : NotSpecified: (:) [], PSSecurityException + FullyQualifiedErrorId : RuntimeExc