
聽起來大多數終端模擬器預設不會執行本機會話作為登錄,因此它們將載入 bashrc 而不是 bash_profile。那麼為什麼大多數人將所有內容放在 bash_profile 中並讓 bashrc 獲取它而不是相反呢?我所說的「大多數人」是指迄今為止我見過的大多數人。也許它並不像我想像的那麼普遍。
與其將我們的配置放在那裡並讓 bashrc 來源 bash_profile,將所有內容都放在 bashrc 中並讓 bash_profile 來源不是更有意義並且與 Linux 社群更加一致嗎?
我聽說過有關 iTerm2 的好消息,聽起來就像這樣,當我在本地運行時,幾乎所有其他終端模擬器(除了預設的 OSX 終端)最終都會加載 bashrc。這並不重要,只要一個來源另一個,但我很困惑為什麼更喜歡 bash_profile 是標準?
小附註:我對 iTerm2 的看法是錯的。它預設運行登入會話,就像 Terminal.app 一樣,儘管兩個模擬器似乎都有一個選項可以讓您更改它。
答案1
人們從 bashrc 取得 bash_profile,而不是相反由於當地慣例。
一切觀點我讀到的關於如何配置啟動文件的內容bash
主要是基於本地約定。本地慣例通常不會提及大局,因為它不會過多談論非登入、非互動的情況。有趣的是,我已經看過了,但我很少看到有人cron
在他們的所有談話中提到為什麼將變數放在一個啟動文件中而不是另一個啟動文件中。事實上,我沒有聽到任何評論說:“/bin/sh 的存在是有原因的。當如此呼叫時,Bash 會模擬原始的 Bourne shell /bin/sh。「一方面,我稍微離題了,這個案例對於那些不僅以互動方式使用 shell,而且提供非互動、非登入功能的人來說很重要(無人看管的或者背景)cron
需要最少 shell 處理的腳本,即後台處理不需要彩色提示、命令歷史記錄和替換、正確定義的 $TERM 變數等細節。
此外,關於cron
,我通常看到的是人們在工作時創建最小的搜尋路徑或調用完全合格的程序,並且不知道如何處理未連接到終端的輸出(即非互動式、非登入bash
或sh
情況)與他們的cron
腳本。這通常是因為沒有充分理解 shell 啟動順序,從而導致使用者以與本機/etc
啟動檔案中已建立的約定不一致或不連貫的方式實現自己的啟動檔案。
詳細來說,按本地約定完成的設定被放置在特定的安裝和 shell/etc
檔案中。如果檢查任何 UNIX 安裝的/etc
檔案(這些檔案是作為典型啟動序列的一部分調用的) ,那麼應該以一種與這些啟動檔案bash
中建立的約定互補的方式建立自己的啟動。/etc
Linux 文件專案指出:
/etc/skel/ 每個新使用者的預設檔案都儲存在該目錄中。每次新增使用者時,這些框架檔案都會複製到其主目錄中。一般系統會有:.alias、.bash_profile、.bashrc 和 .cshrc 檔案。其他文件由系統管理員決定。
雖然bash
手冊沒有明確提及目錄中常見的這些文件/etc/skel
,但據我記得,SunOS、Solaris、RedHat、Ubuntu、HP-UX、umips 和 Ultrix 都有用於/etc/skel
模仿用戶 shell 啟動文件的文件。 OSX 顯然沒有 - 我現在正在使用 OSX 10.9.1。不幸的是,OSX 並沒有給你太多關於如何按照慣例進行設定的信息,但由於 OSX 是 BSD 衍生品,所以我只是使用了另一個 BSD 衍生品,然後設計了我自己的bash
啟動順序,調整它適合OSX 10.9.1/etc
啟動檔案中使用的本地約定。
平行評論中提到的一個重要點是,對於 OSX,慣例是將每個新終端作為互動式登入 shell 啟動。這確實是 OSX 中的預設值。只要安裝的用戶一致,這種約定就沒有問題。 透過對終端首選項進行以下更改,可以更改 OSX 上終端機的預設行為,以符合其他 UNIX 發行版的 shell 啟動約定,特別是更改設置,Shells open with:
發出/usr/bin/login -f -l whmcclos bash -i
命令:
有了這些作為背景或介紹,我將繼續討論我最好的建議, 物有所值。
我最好的建議:
檢查 UNIX 發行管理員已放置的檔案。從以下位置開始(如果存在)。不要忘記使用該ls -a
命令,因為某些檔案以點開頭。查看啟動期間如何使用這些文件,並查看您自己的啟動文件如何與它們互動:
/etc/bashrc
/etc/profile
/etc/skel/.bash_logout
/etc/skel/.bashrc
/etc/bash.bashrc
/etc/bash_completion
需要注意的是,這裡是我在OSX 10.9.1 安裝上的做法- 其他UNIX 發行版會有所不同,但下面介紹的內容應該適用於大多數(如果不是全部)UNIX 發行版,但請使用那些其他UNIX 發行版約定作為指南,根據您自己的目的自訂以下內容:
。
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists. Note, however, that we will have a ~/.bash_profile and it
# will simply source this file as a matter of course.
# See /usr/share/doc/bash/examples/startup-files for examples.
# The files are located in the bash-doc package.
# From here on out, I basically set up my PATH, LD_LIBRARY_PATH, and anything else I'd like
# global to running programs and how those programs find their libraries. This is shared by
# `cron`, so we really don't want interactive stuff, here. Also, I setup my environments
# for brew, macports, and fink here, essentially with setting PATH, and invocation of those
# package initialization file as in:
# Brew and locally compiled stuff:
export PATH=/usr/local/bin:$PATH
export PATH=/usr/local/sbin:$PATH
# The following line puts gnu utilities without the prefix "g" in the path
# i.e. tar/gtar:
export PATH=$PATH:/usr/local/Cellar/coreutils/8.21/libexec/gnubin
# MacPorts shoves stuff in /opt, so to get at that stuff...
export PATH=/opt/local/bin:$PATH
export PATH=/opt/local/sbin:$PATH
# Set up for using Fink, which lives in /sw:
[ -e /sw/bin/init.sh ] && . /sw/bin/init.sh
# My stuff:
export PATH=~/perl:$PATH
export PATH=~/bin:$PATH
export PATH=.:$PATH
.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# From here on out, I put in things that are meaningful to interactive shells, like aliases,
# `shopt` invocations, HISTORY control, terminal characteristics, PROMPT, etc.
.bash_設定檔
# ~/.bash_profile: executed by the command interpreter for login shells.
# Because of this file's existence, neither ~/.bash_login nor ~/.profile
# will be sourced.
# See /usr/share/doc/bash/examples/startup-files for examples.
# The files are located in the bash-doc package.
# Because ~/.profile isn't invoked if this files exists,
# we must source ~/.profile to get its settings:
if [ -r ~/.profile ]; then . ~/.profile; fi
# The following sources ~/.bashrc in the interactive login case,
# because .bashrc isn't sourced for interactive login shells:
case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac
# I'm still trying to wrap my head about what to put here. A suggestion
# would be to put all the `bash` prompt coloring sequence functions as
# described on http://brettterpstra.com/2009/11/17/my-new-favorite-bash-prompt/
這就是我的兩分錢。請記住,我的範例試圖透過啟動文件顯示控制路徑,並避免任何特定網站的約定可能強加的內容。
答案2
為什麼我們先將所有內容都放在 bash_profile 中?
.profile 最初由 /bin/sh 使用,使用 .profile 允許向後相容。
除非您使用的是 Mac,否則所有內容都不會放入 bash_profile 中。它被放在bashrc中
將所有內容放在 bashrc 中並使用 bash_profile 來源不是更有意義並且與 Linux 社群更一致嗎?
通常將系統資訊放入連接到電腦時載入的 shell 中(正常運作時間、需要更新的套件、CPU 溫度等)。如果 bashrc 源自 bash_profile,則每次開啟新 shell 時都會看到所有這些資訊。
在 Unix 中:
.bash_profile 由登入 shell 載入
.bashrc 由互動式 shell 載入
Mac 除外,它會載入登入 shell每個新航廈(互動或非互動)
其他資源
答案3
[...] 我很困惑為什麼更喜歡 bash_profile 是標準?
誰說這是標準?這bash手冊本身就這個問題有這樣的說法:
因此,通常,您的 ~/.bash_profile 包含行
如果 [ -f ~/.bashrc ];然後 。 〜/ .bashrc;菲
在任何特定於登入的初始化之後(或之前)。