Linux

這個 bash shell 別名在哪裡配置?

  • August 1, 2014

以 root 身份登錄到 CentOS 6.5 系統時,我執行

># alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

我想alias ll='ls -l --color=auto'alias ll='ls -lha --color=auto'所有使用者修改。cat /etc/bashrccat /root/.bashrc產量,分別為:

># cat /etc/bashrc
# /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.

# are we an interactive shell?
if [ "$PS1" ]; then
 if [ -z "$PROMPT_COMMAND" ]; then
   case $TERM in
   xterm*)
       if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
           PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
       else
           PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
       fi
       ;;
   screen)
       if [ -e /etc/sysconfig/bash-prompt-screen ]; then
           PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
       else
           PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
       fi
       ;;
   *)
       [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
       ;;
     esac
 fi
 # Turn on checkwinsize
 shopt -s checkwinsize
 [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
 # You might want to have e.g. tty in prompt (e.g. more virtual machines)
 # and console windows
 # If you want to do so, just add e.g.
 # if [ "$PS1" ]; then
 #   PS1="[\u@\h:\l \W]\\$ "
 # fi
 # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
   # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
   pathmunge () {
       case ":${PATH}:" in
           *:"$1":*)
               ;;
           *)
               if [ "$2" = "after" ] ; then
                   PATH=$PATH:$1
               else
                   PATH=$1:$PATH
               fi
       esac
   }

   # By default, we want umask to get set. This sets it for non-login shell.
   # Current threshold for system reserved uid/gids is 200
   # You could check uidgid reservation validity in
   # /usr/share/doc/setup-*/uidgid file
   if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
      umask 002
   else
      umask 022
   fi

   # Only display echos from profile.d scripts if we are no login shell
   # and interactive - otherwise just process them to set envvars
   for i in /etc/profile.d/*.sh; do
       if [ -r "$i" ]; then
           if [ "$PS1" ]; then
               . "$i"
           else
               . "$i" >/dev/null 2>&1
           fi
       fi
   done

   unset i
   unset pathmunge
fi
# vim:ts=4:sw=4

># cat /root/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
       . /etc/bashrc
fi

它是 /etc/profile.d/colorls.sh 包含兩個引用ll

># cat /etc/profile.d/colorls.sh
# color-ls initialization

#when USER_LS_COLORS defined do not override user LS_COLORS, but use them.
if [ -z "$USER_LS_COLORS" ]; then

 alias ll='ls -l' 2>/dev/null
 alias l.='ls -d .*' 2>/dev/null


 # Skip the rest for noninteractive shells.
 [ -z "$PS1" ] && return

 COLORS=

 for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" \
     "$HOME/.dir_colors" "$HOME/.dircolors"; do
   [ -e "$colors" ] && COLORS="$colors" && break
 done

 [ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.256color" ] && \
     [ "x`tty -s && tput colors 2>/dev/null`" = "x256" ] && \
     COLORS="/etc/DIR_COLORS.256color"

 if [ -z "$COLORS" ]; then
   for colors in "/etc/DIR_COLORS.$TERM" "/etc/DIR_COLORS" ; do
     [ -e "$colors" ] && COLORS="$colors" && break
   done
 fi

 # Existence of $COLORS already checked above.
 [ -n "$COLORS" ] || return

 eval "`dircolors --sh "$COLORS" 2>/dev/null`"
 [ -z "$LS_COLORS" ] && return
 grep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null && return
fi

alias ll='ls -l --color=auto' 2>/dev/null
alias l.='ls -d .* --color=auto' 2>/dev/null
alias ls='ls --color=auto' 2>/dev/null

那麼,我是否編輯/etc/profile.d/colorls.sh,如果是,在兩個出現的地方ll?還是有更好的方法來解決它?

如果您的所有使用者都有 .bashrc 並引用執行 /etc/bashrc,那麼在該文件的底部添加一行將是進行此修改覆蓋先前別名的好地方。

在 /etc/bashrc 的底部添加一些註釋和新別名,例如:

# Setting ll alias -- August 2014
# by XXXX

alias ll='ls -lha --color=auto'

我認為這是修改 colorls.sh 的一個乾淨簡單的替代方法。

編輯:

是的你是對的。更好的方法是創建 /etc/profile.d/custom.sh 並在那裡添加別名。

事實上,我應該修正我的答案。

只需創建文件 /etc/profile.d/custom.sh,在其中添加您的自定義設置並為其設置執行標誌。

可能有用的其他資訊:http ://www.thelinuxdaily.com/2010/08/setup-a-universal-user-profile-with-etcprofile-dcustom-sh/

引用自:https://serverfault.com/questions/617206