Linux命令——source


參考:What does 'source' do?

前言

當我們修改了/etc/profile文件,並想讓它立刻生效,而不用重新登錄,就可以使用source命令,如source /etc/profile。

source命令

source是一個內建命令。下面命令執行環境是Ubuntu 16.04.3 LTS下的bash。事實上source命令並不是bash(Bourne shell)內置的,他是GNU項目后來擴展引入的。其他shell不確定是否有source命令。

Linux命令——type、which、whatis、apropos、whereis、find

root@ubuntu:~# type source 
source is a shell builtin
View Code

除了type可以查看source,help命令也可以

root@ubuntu:~# help source 
source: source filename [arguments]
    Execute commands from a file in the current shell.
    
    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.
    
    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.
View Code

但是man,whatis命令就不可以查看source命令了

root@ubuntu:~# man source
No manual entry for source
root@ubuntu:~# whatis source
source: nothing appropriate.
View Code

考慮到和其他shell的兼容性,最好使用點號 . 。點號.是source的同義詞,為啥使用source而不是.,可能的原因是:寫.有時候看起來像是寫錯了的命令,用source更直觀。

但是從簡潔的角度看,實在沒必要打那么多字來實現一個和.一樣的功能

./ 和 source 不完全一樣

  • ./script runs the script as an executable file, launching a new shell to run it
  • source script reads and executes commands from filename in the current shell environment

Note: ./script is not  . script, but . script == source script

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM