Ricordo ampiamente cosa fa, ma non specificamente:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Qual è lo scopo esatto per questo ancora?
Questo origina un file ~/.bashrc se esiste e carica il contenuto di quel file.
La risposta breve alla tua domanda è: hai fatto questo, indipendentemente da come arrivi alla tua macchina, tramite una shell aperta in una sessione desktop interattiva o un accesso ssh alla macchina, la tua shell bash ha una configurazione coerente ad esso.
La risposta più lunga al motivo per cui dovevi farlo in questo modo si trova sotto ...
Il motivo per cui lo si desidera è racchiuso nella cronologia di bash: uno di questi file viene utilizzato per gli accessi interattivi, l'altro è utilizzato per gli accessi non interattivi. Il pensiero alla base di due impostazioni diverse è che potresti desiderare alcune cose nella tua shell interattiva (prompt di fantasia, messaggio del giorno, colorazione del tuo output di comando, ecc.) Che non vorresti in una sessione non interattiva perché fa scattare l'accesso programmatico all'input e all'output e cosa no.
Per la maggior parte degli utenti, anche se entrambi gli ambienti devono essere uguali. La maggior parte di noi non esegue complicate configurazioni bash per garantire la separazione di configurazioni interattive e non interattive come questa.
Il .bash_profile viene eseguito per le shell di login, mentre .bashrc viene eseguito per le shell interattive non di login.
C'è un bel articolo di Josh Staiger che parla del perché uno sull'altro e di come ricordare dove metti le cose . Consentitemi di citarne alcuni qui:
What is a login or non-login shell?
When you login (type username and password) via console, either sitting at the machine, or remotely via
ssh:.bash_profileis executed to configure your shell before the initial command prompt. But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then.bashrcis executed before the window command prompt..bashrcis also run when you start a new bash instance by typing/bin/bashin a terminal.Mac OS X — an exception
An exception to the terminal window guidelines is Mac OS X’s
Terminal.app, which runs a login shell by default for each new terminal window, calling.bash_profileinstead of.bashrc. Other GUI terminal emulators may do the same, but most tend not to.Recommendation
Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a PATH, you want it to apply to both. You can fix this by sourcing
.bashrcfrom your.bash_profilefile, then putting PATH and common settings in.bashrc.To do this, add the following lines to .bash_profile:
if [ -f ~/.bashrc ]; then source ~/.bashrc fiNow when you login to your machine from a console
.bashrcwill be called.
Vedi la descrizione di Ian C su cosa fa la linea sorgente
Questo è circondato da un'istruzione if
if x; then
....
fi
questo controlla x e quindi esegue il codice tra il then e il fi (L'idea di fi-end e if e altri endins invertendo la parola chiave beggining è usata nelle shell sh e correlate.
Il controllo che viene fatto qui è [ -f ~/.bashrc ] che è uguale a test -f .bashrc se dice a test di restituire true iff cosa segue -f è un file ed esiste.
Quindi il codice sopra dice se c'è un .bashrc che lo esegue nella tua shell cirrent.
Leggi altre domande sui tag command-line macos terminal