是什么`sh`和`source`之间的区别? [英] What is the difference between `sh` and `source`?

查看:120
本文介绍了是什么`sh`和`source`之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 SH 之间的差异,

来源:源文件名[参数]
    读取和文件名,并返回执行命令。路径名
    在$ PATH用于查找包含路径文件名。如果有任何
    提供参数,成为他们的位置参数时,
    文件名被执行。

和对男人SH

NAME
       庆典 - GNU的Bourne Again Shell的概要
       bash的[选项] [文件]版权
       Bash是版权所有(C)1989-2004自由软件基金会,公司描述
       Bash是一个sh兼容的命令语言间preTER是执行从标准输入或从文件中读取命令。 Bash也整合
       从Korn和C shell的实用功能(KSH和csh)。       庆典旨在成为符合了IEEE POSIX壳牌和工具规范(IEEE 1003.2工作组)的。


解决方案

当你调用(或其别名),你的插入的在电流的bash进程的脚本。所以你可以读由脚本设置变量。

当你调用 SH ,你发起的的(子进程)运行的一个新的会话 / bin / sh的,这通常是一个符号链接庆典。在这种情况下,当子脚本完成由子脚本中设置的环境变量将被丢弃。

注意 SH 可能是一个符号链接的其他的壳

一个小样本

例如,如果你想改变的当前工作目录的由一个特定的方式,你不能这样做。

 猫<<&EOF GT; myCd2Doc.sh
#!/ bin / sh的
CD的/ usr /共享/ DOC
EOF搭配chmod + X myCd2Doc.sh

这不会做你所期望的:

  CD / tmp目录
PWD
/ tmp目录
〜/ myCd2Doc.sh
PWD
/ tmp目录

由于的当前工作目录的是环境的一部分, myCd2Doc.sh 将在子shell 的运行。

不过:<​​/ P>

 猫&GT; myCd2Doc.source&LT;&LT; EOF
#壳牌源文件
myCd2Doc(){
    CD的/ usr /共享/ DOC
}
EOF。 myCd2Doc.source
CD / tmp目录
PWD
/ tmp目录
myCd2Doc
PWD
在/ usr /共享/ DOC

我写的一个小样本 mycd 功能。

What is the difference between sh and 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.  If any
    ARGUMENTS are supplied, they become the positional parameters when
    FILENAME is executed.

And for man sh:

NAME
       bash - GNU Bourne-Again SHell

SYNOPSIS
       bash [options] [file]

COPYRIGHT
       Bash is Copyright (C) 1989-2004 by the Free Software Foundation, Inc.

DESCRIPTION
       Bash  is  an sh-compatible command language interpreter that executes commands read from the standard input or from a file.  Bash also incorporates
       useful features from the Korn and C shells (ksh and csh).

       Bash is intended to be a conformant implementation of the IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2).

解决方案

When you call source (or its alias .), you insert the script in the current bash process. So you could read variables set by the script.

When you call sh, you initiate a fork (sub-process) that runs a new session of /bin/sh, which is usually a symbolic link to bash. In this case, environment variables set by the sub-script would be dropped when the sub-script finishes.

Caution: sh could be a symlink to another shell.

One little sample

For example, if you want to change current working directory by a specific manner, you could not do

cat <<eof >myCd2Doc.sh
#!/bin/sh
cd /usr/share/doc
eof

chmod +x myCd2Doc.sh

This won't do what you expect:

cd /tmp
pwd
/tmp
~/myCd2Doc.sh
pwd
/tmp

because current working dir is part of environment and myCd2Doc.sh would run in a subshell.

But:

cat >myCd2Doc.source <<eof
# Shell source file
myCd2Doc() {
    cd /usr/share/doc
}
eof

. myCd2Doc.source
cd /tmp
pwd
/tmp
myCd2Doc
pwd
/usr/share/doc

I wrote a small sample of mycd function.

这篇关于是什么`sh`和`source`之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆