linux bash source里source,sh,bash,/有什么区别

source、sh、bash、./执行脚本的区别
1、source命令用法:
  source FileName
  作用:在当前bash环境下读取并执行FileName中的命令。该filename文件可以无"执行权限"
注:该命令通常用命令“.”来替代。
如:source .bash_profile
. .bash_profile两者等效。
source(或点)命令通常用于重新执行刚修改的初始化文档。
source命令(从 C Shell 而来)是bash shell的内置命令。
点命令,就是个点符号,(从Bourne Shell而来)。
2、sh和bash命令用法:
sh FileName
bash FileName
作用:在当前bash环境下读取并执行FileName中的命令。该filename文件可以无"执行权限"
注:两者在执行文件时的不同,是分别用自己的shell来跑文件。
sh使用“-n”选项进行shell脚本的语法检查,使用“-x”选项实现shell脚本逐条语句的跟踪,
可以巧妙地利用shell的内置变量增强“-x”选项的输出信息等。
3、./的命令用法:
./FileName
作用:打开一个子shell来读取并执行FileName中命令。
注:运行一个shell脚本时会启动另一个命令解释器.
每个shell脚本有效地运行在父shell( shell)的一个子进程里.
这个父shell是指在一个控制终端或在一个xterm窗口中给你命令指示符的进程.
shell脚本也可以启动他自已的子进程.
这些子shell(即子进程)使脚本并行地,有效率地地同时运行脚本内的多个子任务.
shell的嵌入命令:
: 空,永远返回为true.
从当前shell中执行操作break 退出for、while、until或case语句cd 改变到当前目录continue 执行循环的下一步echo 反馈信息到标准输出eval 读取参数,执行结果命令exec 执行命令,但不在当前shellexit 退出当前shellexport 导出变量,使当前shell可利用它pwd 显示当前目录read 从标准输入读取一行文本
readonly 使变量只读return 退出函数并带有返回值set 控制各种参数到标准输出的显示shift 命令行参数向左偏移一个test 评估条件表达式times 显示shell运行过程的用户和系统时间trap 当捕获信号时运行指定命令ulimit 显示或设置shell资源umask 显示或设置缺省文件创建模式unset 从shell内存中删除变量或函数wait 等待直到子进程运行完毕
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!博客分类:
我使用过的Linux命令之source - 在当前shell环境中执行指定文件中的命令
本文链接:
(转载请注明出处)
source命令是bash中的内建命令,它等同于点命令(.),用于读取和在当前shell环境中执行指定文件中的命令,执行完毕之后退出码为该文件中的最后一个命令的退出码(Read
execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.)。指定的文件可以没有执行权限。
在当前shell中执行和在子shell中执行的区别是,后者定义的变量和函数在执行结束后就消失了,而前者却可以保留下来。有时候我们修改了/etc/profile里面的内容,如增加了环境变量,那么要立即生效的话,就必须使用source命令或者点命令在当前shell中执行一下。
filename [arguments]
格式: source filename [arguments]
在后面的示例中会分别对各种情况举例演示。
[root@web imx_web3q]# help source
source: source filename [arguments]
Read and execute commands from FILENAME and return.
The pathnames
in $PATH are used to find the directory containing FILENAME.
ARGUMENTS are supplied, they become the positional parameters when
FILENAME is executed.
[root@web imx_web3q]#
示例二 修改/etc/profile之后使之立即生效
[root@web imx_web3q]# vi /etc/profile
[root@web imx_web3q]# . /etc/profile
[root@web imx_web3q]#
示例三 在PATH中搜索命令
source中说道:如果filename不包含斜杠(/),那么从PATH环境变量指定的那些路径搜索filename,这个文件不必是可执行
的。(If filename does not contain a slash, file names in
find the directory containing filename.
The file searched for in
PATH need not be
executable.)如果在PATH中找不到指定文件,当bash不是posix模式时,将在当前目录下搜索该文件。(When bash is
not in posix mode, the current directory is searched if no file is found
in PATH.)如果shopt里的sourcepath关闭,则不在PATH中搜索指定文件。(If
the sourcepath
the shopt builtin command is turned off, the PATH is not searched.)
[root@new55 ~]# shopt
cdable_vars
checkwinsize
expand_aliases
force_fignore
gnu_errfmt
histappend
histreedit
histverify
hostcomplete
interactive_comments
login_shell
no_empty_cmd_completion off
nocaseglob
nocasematch
promptvars
restricted_shell
shift_verbose
offsourcepath
[root@new55 ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/opt/apache/apache-ant-1.8.1/bin:/usr/java/jdk1.6.0_21/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
:/root/bin
[root@new55 ~]# ls -l /usr/bin/sj.sh
-rwxr-xr-x 1 root root 453 09-15 04:46 /usr/bin/sj.sh
[root@new55 ~]# cat /usr/bin/sj.sh
listpids()
#ps -ef|grep java|grep -v grep
COLUMNS=1024 ps h -C java -f
showpids()
while read u p pp t1 t2
ls -l /proc/$p/cwd
echo $p $cwd $cmd
showpidof()
while read u p pp t1 t2
if ls -l /proc/$p/cwd | grep -q $1; then
elif echo $cmd | grep -q $1; then
if [ "$1" ]; then
listpids | showpidof $1 | xargs
listpids | showpids
[root@new55 ~]# sj.sh
lrwxrwxrwx 1 root root 0 12-09 19:11 /proc/6832/cwd -& /root/work55/cms_server
/usr/java/jdk1.6.0_21/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start
[root@new55 ~]# listpids
-bash: listpids: command not found
[root@new55 ~]# chmod -x /usr/bin/sj.sh
[root@new55 ~]# source sj.sh
lrwxrwxrwx 1 root root 0 12-09 19:11 /proc/6832/cwd -& /root/work55/cms_server
/usr/java/jdk1.6.0_21/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start
[root@new55 ~]# listpids
0 19:11 pts/2
0:02 /usr/java/jdk1.6.0_21/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp
[root@new55 ~]# chmod +x /usr/bin/sj.sh
root@new55 ~]#
示例四 位置参数
If any arguments are supplied, they become the positional parameters when
the positional
parameters are unchanged.
[root@new55 ~]# cat &source.sh
echo "args: $@"
[root@new55 ~]# ./source.sh
-bash: ./source.sh: 权限不够
[root@new55 ~]# . source.sh
[root@new55 ~]# echo $XYZ
[root@new55 ~]# . source.sh hello world
args: hello world
[root@new55 ~]#
示例五 退出码
The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.
[root@new55 ~]# ls x.sh
ls: x.sh: 没有那个文件或目录
[root@new55 ~]# . ./x.sh
-bash: ./x.sh: 没有那个文件或目录
[root@new55 ~]# echo $?
[root@new55 ~]# cat &x.sh
[root@new55 ~]# . ./x.sh
[root@new55 ~]# echo $?
[root@new55 ~]#
【1】笑遍世界的测试技术
【2】Simon的专栏
【3】LinuxSense
【4】jiakechong
浏览 22605
codingstandards
浏览: 3803424 次
来自: 上海
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
楼主咋没分析下源码呢?
tail -F 就可以吧
新手学习了,就是不明白为一个网卡配多个ip有什么用
不错,谢谢!
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'source、sh、bash、./
执行shell脚本 区别
在linux里,source、sh、bash、./都可以执行shell script文件,那它们有什么不同吗?-----------1、sourcesource a.sh在当前shell内去读取、执行a.sh,而a.sh不需要有"执行权限"source命令可以简写为".". a.sh注意:中间是有空格的。 2、sh/bashsh a.sh
bash a.sh都是打开一个subshell去读取、执行a.sh,而a.sh不需要有"执行权限"通常在subshell里运行的脚本里设置变量,不会影响到父shell的。 3、././a.sh
#bash: ./a.sh: 权限不够
chmod +x a.sh
./a.sh打开一个subshell去读取、执行a.sh,但a.sh需要有"执行权限"可以用chmod +x添加执行权限 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
echo "PID for 1.sh before exec/source/fork:$$"
echo "1.sh: \$A is $A"
case $1 in
echo "using exec..."
exec ./2.sh ;;
echo "using source..."
. ./2.sh ;;
echo "using fork by default..."
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"
echo "2.sh: \$A is $A"自己运行下,观看结果吧 :)chmod +x 1.sh
chmod +x 2.sh
./1.sh fork
./1.sh source
./1.sh execshell中使用source conf.sh,是直接运行conf.sh的命令,不创建子shell,类似与html中includesh是则创建子shell,子shell里面 的变量父shell无法使用,对环境变量的修改也不影响父shell。父shell中的局部变量,子shell也无法使用,只有父shell的环境变量, 子shell能够使用。还有一个exec conf.sh,这个与source类似,区别是exec执行完,不再执行后面的语句。还有一个.号,用来引用其他文件(一般定义了一些function,和变量)1.我们所执行的任何程序,都是由父进程(parent process)所产生出来的一个子进程(child process),子进程在结束后,将返回到父进程去。此一现像在Linux系统中被称为 fork。当子进程被产生的时候,将会从父进程那里获得一定的资源分配、及(更重要的是)继承父进程的环境﹗ 2.Shell变量大致可以分为3种类型:
内部变量:系统提供,不用定义,不能修改,比如$#,$?,$*,$0等
环境变量:系统提供,不用定义,可以修改,当前进程及其子进程中使用,比如PATH,PWD,SHELL等
用户变量(本地变量):用户定义,可以修改,在当前进程使用,比如var=123等
与其他语言的区别:非类型性质,也就是不必指定变量是数字或字符串等。 3.关于环境变量:环境变量只能从父进程到子进程单向继承。换句话说:在子进程中的环境如何变更,均不会影响父进程的环境。 4.Shell脚本:其实就是将你平时在Shell prompt后所输入的多行command依序写入一个文件去而已 fork,source和exec方式执行Shell脚本 fork方式 也就是常用的方式,一般在shell直接输入脚本文件路径就可以了。这种方式由当前进程创建一个子进程1:
./mytest.shsource方式 使用方式如下(source与".”等价):1:
source ./mytest.sh2:
../mytest.shsource方式的特点是,在不另外创建子进程,而是在当前的的Shell环境中执行。exec方式1:
exec./mytest.sh此方式的特点是,不另外创建子进程,但是会终止当前的shell执行(其实我觉得这样理解可能更准确:使用exec会在当前的进程空间创建一个子线程,然后终止当前线程的执行,到了新建的线程执行完之后,其实两个线程都终止了,也就是这个当前shell进程也就终止了) 参考博客:https://www.cnblogs.com/pcat/p/5467188.html
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!linux中source是什么指令?_百度知道
linux中source是什么指令?
我有更好的答案
命令用法:source FileName作用:在当前bash环境下读取并执行FileName中的命令。注:该命令通常用命令“.”来替代。如:source /etc/profile 与 . /etc/profile是等效的。注意:source命令与shell scripts的区别是,source在当前bash环境下执行命令,而scripts是启动一个子shell来执行命令。这样如果把设置环境变量(或alias等等)的命令写进scripts中,就只会影响子shell,无法改变当前的BASH,所以通过文件(命令列)设置环境变量时,要用source 命令。
采纳率:64%
来自团队:
为您推荐:
其他类似问题
linux的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。抖腿练习中
ubuntu下source、sh、bash、./执行脚本的区别
source命令用法:
  source FileName
  作用:在当前bash环境下读取并执行FileName中的命令。该filename文件可以无”执行权限”
注:该命令通常用命令“.”来替代。
如:source bash_profile
bash_profile两者等效。
source(或点)命令通常用于重新执行刚修改的初始化文档。
source命令(从 C Shell 而来)是bash shell的内置命令。
点命令,就是个点符号,(从Bourne Shell而来)。
source的程序主体是bash,脚本中的$0变量的值是bash,而且由于作用于当前bash环境,脚本中set的变量将直接起效
sh, bash的命令用法:
sh/bash FileName
作用:打开一个子shell来读取并执行FileName中命令。该filename文件可以无"执行权限"
注:运行一个shell脚本时会启动另一个命令解释器.
每个shell脚本有效地运行在父shell(parent shell)的一个子进程里.
这个父shell是指在一个控制终端或在一个xterm窗口中给你命令指示符的进程.
shell脚本也可以启动他自已的子进程.
这些子shell(即子进程)使脚本并行地,有效率地地同时运行脚本内的多个子任务.
在ubuntu中sh只是bash的一个链接。
由于是在子shell中执行,脚本设置的变量不会影响当前shell。
./的命令用法:
./FileName
作用:打开一个子shell来读取并执行FileName中命令。该filename文件需要"执行权限"
注:运行一个shell脚本时会启动另一个命令解释器.
每个shell脚本有效地运行在父shell(parent shell)的一个子进程里.
这个父shell是指在一个控制终端或在一个xterm窗口中给你命令指示符的进程.
shell脚本也可以启动他自已的子进程.
这些子shell(即子进程)使脚本并行地,有效率地地同时运行脚本内的多个子任务.
由于是在子shell中执行,脚本设置的变量不会影响当前shell。
现象: shell脚本中source ***.shor source ~/.bashrc时提示 source: not found
原因: ls -l which sh 提示/bin/sh -& dash
这说明是用dash来进行解析的。
改回方法:
命令行执行:sudo dpkg-reconfigure dash
在界面中选择no
再ls -l which sh 提示/bin/sh -& bash
修改成功,source可以用了~
Dash Shell 是一个Unix Shell,比Bash Shell 小的多,符合POSIX标准。Debian曾经采用/bin/bash的更改/bin/dash,目的使用更少的磁盘空间、提供较少的功能、获取更快的速度。但是后来经过shell脚本测试存在运行问题。因为原先在bash shell 下可以运行的shell script (shell 脚本),会出现一些意想不到的问题,不是100%的兼用。
Ubuntu继承了Debian,所以从Ubuntu 6.10开始默认是Dash Shell。
解决办法:重新配置shell
1. sudo dpkg-reconfigure dash
选择NO,不使用Dash Shell。
2.采用链接指向
ln -s /bin/bash /bin/sh
检查是否正确:ls -l /bin/sh
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 bash source 0 的文章

 

随机推荐