首頁>Program>source

sh和有什麼不一樣 和 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.

對於 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).
最新回復
  • 5月前
    1 #

    当您致電 source (或其別名 . ),您可以在 current插入指令碼 打击過程.因此,您可以讀取指令碼設置的變數。

    当您致電 sh ,您將啟動fork(子過程),该子過程將執行新的 /bin/sh会话 ,通常是到 bash的符號鏈接 .在這種情况下,子指令碼完成時將删除由子指令碼設置的環境變數。

    Cautionsh 可能是另一个外壳的符號鏈接。

    一个小樣本

    例如,如果要以特定方式更改当前工作目錄,則不能

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

    這不会達到您的期望:

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

    因為当前工作目錄是環境的一部分,所以 將在subshel​​l中執行。

    但是:

    myCd2Doc.sh
    

    (我寫了一个 cat >myCd2Doc.source <<eof # Shell source file myCd2Doc() { cd /usr/share/doc } eof . myCd2Doc.source cd /tmp pwd /tmp myCd2Doc pwd /usr/share/doc的小樣本 函式。)

    執行級別 mycd
    $SHLVL
    
    很少遞迴
    cd /tmp
    printf %b '\43\41/bin/bash\necho This is level \44SHLVL.\n' >qlvl.sh
    bash qlvl.sh 
    This is level 2.
    source qlvl.sh 
    This is level 1.
    
    最终測試:
    cat <<eoqlvl >qlvl.sh 
    #!/bin/bash
    export startLevel
    echo This is level $SHLVL starded:${startLevel:=$SHLVL}.
    ((SHLVL<5)) && ./qlvl.sh
    eoqlvl
    chmod +x qlvl.sh
    ./qlvl.sh 
    This is level 2 starded:2.
    This is level 3 starded:2.
    This is level 4 starded:2.
    This is level 5 starded:2.
    source qlvl.sh 
    This is level 1 starded:1.
    This is level 2 starded:1.
    This is level 3 starded:1.
    This is level 4 starded:1.
    This is level 5 starded:1.
    

    ...您可能会註意到两種語法之間的不同行為. ;-)

  • 5月前
    2 #

    主要區別在於它们是在不同的程序中執行的。

    所以,如果你 printf %b '\43\41/bin/bash\necho Ending this.\nexit 0\n' >finalTest.sh bash finalTest.sh Ending this. source finalTest.sh Ending this. 檔案 source 哪个好 ,采购外壳(例如您在终端中的互動式外壳)会受到影响(並且其当前目錄也会更改)

    如果執行 foo cd 不影响采购外壳,仅影响新建立的 sh foo 程序執行 cd

    阅讀Advanced Bash指令碼指南。

    這種差異並非特定於Linux; 每个Posix實現都会拥有它。

  • 5月前
    3 #

    正如其他人所提到的,当您執行 sh時 ,對 foo所做的任何更改 在此過程結束後,對您的Shell環境造成的影响將不会持續。

    但是,還請註意,您的環境中任何未匯出的元素(例如變數,別名和shell函式)將對 sh test.sh中的代碼不可用 当它作為子流程執行時(即使用 test.sh )。

    例如:

    test.sh
    

    示例2:

    sh test.sh
    

  • 5月前
    4 #

    使用sh命令執行程式時:

    $ cat > test.sh echo $foo $ foo=bar $ sh test.sh $ . test.sh bar

    以及使用源命令時:

    [email protected]:~$ cat test.sh #!/bin/sh cd /etc [email protected]:~$ sh test.sh [email protected]:~$ pwd /home/savoury [email protected]:~$ source test.sh [email protected]:/etc$ pwd /etc [email protected]:/etc$ wy

      your terminal will use sh or Bourne Shell to execute the program .

      a new process is created because Bash makes an exact copy of itself . this child process has the same environment as its parent, only the process ID number is different. (this process called forking )

      you need to need to have execution permission to execute it ( since it is forking )

    (或。)-在当前外壳中執行並更改其屬性/環境。

      you execute the program with your default interpreter

      you execute the process in your current terminal ( technically your *nix command interpreted )

      Since the program will be executed in current terminal you dont need to give it execution permission

    进行分叉並在子shell中執行,因此無法更改屬性/環境。

    source

    sh

    For example
    

    My shell script is -

    elite12!rg6655:~/sh_pr [33]$ cat changeDir.sh
    #!/bin/bash
    cd /home/elt/rg6655/sh_pr/justdir
    pwd
    echo $$
    

    我当前shell的程序ID是3272

    My Current Shell -

    elite12!rg6655:~/sh_pr [32]$ echo $$
    3272
    

    观察者两件事- 1)程序ID(3272)与我的Shell相同,它確认源代碼在当前Shell中執行。 2)cd命令起作用,目錄更改為justdir。

    Running with thesource-

    elite12!rg6655:~/sh_pr [34]$ source changeDir.sh
    /home/elt/rg6655/sh_pr/justdir
    3272
    elite12!rg6655:~/sh_pr/justdir
    

    在這種情况下,程序ID(13673)是不同的,並且目錄保持不變,這意味着它在不同的程序或子Shell中執行。

    Running withsh-

  • objective c:如何使用iOS获取UIKeyboard大小
  • python:如何告诉distutils使用gcc?