Dre*_*rew 75 bash login .profile
我是 Ubuntu 的新手。我正在运行 13.10 桌面版。
我想为 bash 设置一些系统范围的别名和自定义提示。我找到了这篇文章:
https://help.ubuntu.com/community/EnvironmentVariables
按照本文中的建议,我创建了 /etc/profile.d/profile_local.sh。它归 root 所有,并且与那里的其他脚本一样具有 644 的权限:
root@ubuntu:/etc/profile.d# ll
total 28
drwxr-xr-x 2 root root 4096 Mar 23 08:56 .
drwxr-xr-x 135 root root 12288 Mar 23 09:15 ..
-rw-r--r-- 1 root root 660 Oct 23 2012 bash_completion.sh
-rw-r--r-- 1 root root 3317 Mar 23 07:36 profile_local.sh
-rw-r--r-- 1 root root 1947 Nov 23 00:57 vte.sh
Run Code Online (Sandbox Code Playgroud)
我进一步确认 /etc/profile 调用 /etc/profile.d。它包含以下代码块:
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
Run Code Online (Sandbox Code Playgroud)
登录后,我创建的自定义脚本 profile_local.sh 似乎没有来源。但是,如果登录后我'source /etc.profile.d/profile_local.sh',我会得到预期的行为、我的自定义别名和自定义提示。
我究竟做错了什么?
脚本“profile_local.sh”的内容:
# 3/23/14 - Copied from Gentoo /etc/bash/bashrc
# Placed in /etc/profile.d as described at:
# https://help.ubuntu.com/community/EnvironmentVariables
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
# Bash won't get SIGWINCH if another process is in the foreground.
# Enable checkwinsize so that bash will check the terminal size when
# it regains control. #65623
# http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11)
shopt -s checkwinsize
# Enable history appending instead of overwriting. #139609
shopt -s histappend
# Change the window title of X terminals
case ${TERM} in
xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'
;;
screen)
PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\033\\"'
;;
esac
use_color=false
# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS. Try to use the external file
# first to take advantage of user additions. Use internal bash
# globbing instead of external grep binary.
safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
match_lhs=""
[[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs} ]] \
&& type -P dircolors >/dev/null \
&& match_lhs=$(dircolors --print-database)
[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
if ${use_color} ; then
# Enable colors for ls, etc. Prefer ~/.dir_colors #64489
if type -P dircolors >/dev/null ; then
if [[ -f ~/.dir_colors ]] ; then
eval $(dircolors -b ~/.dir_colors)
elif [[ -f /etc/DIR_COLORS ]] ; then
eval $(dircolors -b /etc/DIR_COLORS)
fi
fi
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
else
PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
fi
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
else
if [[ ${EUID} == 0 ]] ; then
# show root@ when we don't have colors
PS1='\u@\h \W \$ '
else
PS1='\u@\h \w \$ '
fi
fi
# Try to keep environment pollution down, EPA loves us.
unset use_color safe_term match_lhs
TZ="PST8PDT"
alias ll='ls -la'
alias dig='dig +search'
alias dir='ls -ba'
alias edit="ee"
alias ss="ps -aux"
alias dot='ls .[a-zA-Z0-9_]*'
alias news="xterm -g 80x45 -e trn -e -S1 -N &"
alias more="less"
alias c="clear"
alias m="more"
alias j="jobs"
# common misspellings
alias mroe=more
alias pdw=pwd
Run Code Online (Sandbox Code Playgroud)
ter*_*don 129
要了解这里发生了什么,您需要了解有关 shell(在本例中为 bash)如何运行的一些背景信息。
当您打开终端模拟器(gnome-terminal
例如)时,您正在执行所谓的交互式非登录shell。
当您从命令行、通过ssh
或运行诸如 之类的命令登录到您的计算机时su - username
,您正在运行一个交互式登录shell。
当您以图形方式登录时,您正在运行完全不同的东西,详细信息将取决于您的系统和图形环境,但通常是图形外壳处理您的登录。虽然许多图形 shell(包括 Ubuntu 默认)会读取/etc/profile
不是所有的。
最后,当您运行 shell 脚本时,它会在非交互式、非登录 shell 中运行。
现在,bash 在启动时将读取的文件取决于它正在运行的 shell 的类型。以下是man bash
(强调我的)的 INVOCATION 部分的摘录:
当 bash 作为交互式登录 shell或作为具有 --login 选项的非交互式 shell调用时,它首先从文件/etc/profile读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的命令中读取和执行命令。当 shell 启动时可以使用 --noprofile 选项来禁止这种行为。
当一个交互的shell是不是登录shell 启动时,bash读取并执行命令/etc/bash.bashrc和〜/ .bashrc中,如果这些文件存在。这可以通过使用 --norc 选项来禁止。--rcfile 文件选项将强制 bash 从文件而不是 /etc/bash.bashrc 和 ~/.bashrc 读取和执行命令。
这一切意味着您正在编辑错误的文件。您可以通过删除使用虚拟控制台测试此Ctrl+ Alt+ F2(与返回到GUI Alt+ F7,或F8根据您的设置),并在那里登陆。您将看到您的提示和别名可用。
因此,为了将您想要的设置应用于非登录 shell,即每次打开终端时获得的类型,您应该进行更改~/.bashrc
。或者,您也可以将别名放在文件中~/.bash_aliases
(但是,请注意这是一个 Ubuntu 功能,您不应该期望它在其他发行版上工作)。
有关哪个文件应该用于什么的更多详细信息,请参见此处。
笔记:
Debian(以及扩展 Ubuntu)也有默认的~/.profile
源~/.bashrc
. 这意味着您所做的任何更改~/.bashrc
也将被登录 shell 继承,但是 i) 并非所有 Linux/Unix 机器都是这种情况,ii) 反之亦然,这就是为什么您通常应该始终使用~/.bashrc
&co 而不是~/.profile
or /etc/profile
.
此外,关于使用的一般说明,对配置文件所做的更改/etc
将影响所有用户。这通常不是您想要做的,应该避免。您应该始终使用主目录 ( ~/
) 中的等效文件。
依次读取各种配置文件。具体来说,对于登录shell,顺序是:
/etc/profile -> /etc/profile.d/* (in alphabetical order) -> ~/.bash_profile -> ~/.bash_login -> ~/.profile
Run Code Online (Sandbox Code Playgroud)
这意味着任何设置都~/.profile
将覆盖之前文件中设置的任何内容。
小智 10
在 Debian 的终端会话中,我为所有用户解决了这个问题,因此:
添加到
sudo nano /etc/bash.bashrc
Run Code Online (Sandbox Code Playgroud)
堵塞
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
Run Code Online (Sandbox Code Playgroud)
从
/etc/profile
Run Code Online (Sandbox Code Playgroud)