是什么`sh`和`source`之间的区别?

编程入门 行业动态 更新时间:2024-10-11 11:20:27
本文介绍了是什么`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 /共享/ DOCEOF搭配chmod + X myCd2Doc.sh

这不会做你所期望的:

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

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

不过:

猫> myCd2Doc.source<< EOF#壳牌源文件myCd2Doc(){    CD的/ usr /共享/ DOC}EOF。 myCd2Doc.sourceCD / tmp目录PWD/ tmp目录myCd2DocPWD在/ 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`之间的区别?

本文发布于:2023-11-25 21:15:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   sh   source

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!