歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> ~/.bash_profile等腳本的初始化

~/.bash_profile等腳本的初始化

日期:2017/3/3 10:59:01   编辑:Linux技術

網絡解釋:

在登錄Linux時要執行文件的過程如下:

剛登錄Linux時,首先啟動 /etc/profile 文件,然後再啟動用戶目錄下的 ~/.bash_profile、~/.bash_login或 ~/.profile文件中的其中一個,執行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的話,一般還會執行 ~/.bashrc文件。

執行順序為:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc ->/etc/bashrc -> ~/.bash_logout

[code]/etc/profile:此文件為系統的每個用戶設置環境信息,當用戶第一次登錄時,該文件被執行.並從/etc/profile.d目錄的配置文件中搜集shell的設置.

英文描述為:

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

/etc/bashrc:為每一個運行bash shell的用戶執行此文件.當bash shell被打開時,該文件被讀取.

英文描述為:

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

如果你想對所有的使用bash的用戶修改某個配置並在以後打開的bash都生效的話可以修改這個文件,修改這個文件不用重啟,重新打開一個bash即可生效。

~/.bash_profile:每個用戶都可使用該文件輸入專用於自己使用的shell信息,當用戶登錄時,該文件僅僅執行一次!默認情況下,他設置一些環境變量,執行用戶的.bashrc文件.

此文件類似於/etc/profile,也是需要需要重啟才會生效,/etc/profile對所有用戶生效,~/.bash_profile只對當前用戶生效。

~/.bashrc:該文件包含專用於你的bash shell的bash信息,當登錄時以及每次打開新的shell時,該文件被讀取.(每個用戶都有一個.bashrc文件,在用戶目錄下)

此文件類似於/etc/bashrc,不需要重啟生效,重新打開一個bash即可生效,  /etc/bashrc對所有用戶新打開的bash都生效,但~/.bashrc只對當前用戶新打開的bash生效。

~/.bash_logout:當每次退出系統(退出bash shell)時,執行該文件. 

另外,/etc/profile中設定的變量(全局)的可以作用於任何用戶,而~/.bashrc等中設定的變量(局部)只能繼承/etc/profile中的變量,他們是"父子"關系.

~/.bash_profile 是交互式、login 方式進入bash 運行的;
~/.bashrc 是交互式 non-login 方式進入bash 運行的;
通常二者設置大致相同,所以通常前者會調用後者。
我看了一下我的用戶目錄下

[code].bash_history  .bash_logout   .bash_profile  .bashrc
其中.bash_profile .bashrc 我主要用來配置環境變量了(如java jre androidstudio phpstorm composer)

還要記一點: 如何添加開機啟動腳本

編輯rc.loacl腳本

Ubuntu開機之後會執行/etc/rc.local文件中的腳本,所以我們可以直接在/etc/rc.local中添加啟動腳本。當然要添加到語句:exit 0 前面才行。

[code]#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/www/wdlinux/mongodb/bin/mongod --dbpath /www/wdlinux/mongodb/data --logpath /www/wdlinux/mongodb/log/dblog --port 27017 --auth

exit 0

Copyright © Linux教程網 All Rights Reserved