侧边栏壁纸
博主头像
随心记录

我与旧事归于尽,来年依旧迎花开!

  • 累计撰写 23 篇文章
  • 累计创建 1 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

一个友好的Debian终端配置

晚来听风
2024-12-23 / 0 评论 / 0 点赞 / 6 阅读 / 0 字
温馨提示:
部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1. 非交互式模式下的处理

[ -z "$PS1" ] && return
  • 如果 $PS1 变量为空,说明当前 bash 会话是非交互式的(例如,在执行脚本时),此时直接返回,不执行后续的配置。

2. 历史记录控制

HISTCONTROL=ignoredups:ignorespace
  • 设置历史记录控制变量 HISTCONTROL,忽略重复的命令和以空格开头的命令。

3. 历史记录文件的追加模式

shopt -s histappend
  • 启用 histappend 选项,使得每次退出 bash 时,历史记录会追加到 ~/.bash_history 文件中,而不是覆盖它。

4. 历史记录长度设置

HISTSIZE=1000
HISTFILESIZE=2000
  • 设置 HISTSIZE 为 1000,表示内存中保存的历史命令数量。

  • 设置 HISTFILESIZE 为 2000,表示 ~/.bash_history 文件中保存的历史命令数量。

5. 窗口大小检查

shopt -s checkwinsize
  • 启用 checkwinsize 选项,使得在每次命令执行后检查窗口大小,并更新 LINESCOLUMNS 变量。

6. less 命令的友好处理

[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
  • 如果 /usr/bin/lesspipe 可执行,则使用 lesspipe 来处理非文本文件,使得 less 命令可以更好地显示这些文件。

7. chroot 环境变量的设置

if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi
  • 如果当前环境是 chroot 环境,并且 /etc/debian_chroot 文件可读,则读取该文件内容并赋值给 debian_chroot 变量。

8. 终端颜色支持的检测

case "$TERM" in
    xterm-color) color_prompt=yes;;
esac
  • 根据 $TERM 变量的值判断终端是否支持颜色输出。如果 $TERMxterm-color,则设置 color_promptyes

9. 强制颜色提示符的设置

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        color_prompt=yes
    else
        color_prompt=
    fi
fi
  • 如果 force_color_prompt 变量被设置,则检查 tput 命令是否可用,并尝试设置前景色为红色。如果成功,则设置 color_promptyes

10. 提示符的设置

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
  • 如果 color_promptyes,则设置带有颜色的提示符。

  • 否则,设置不带颜色的提示符。

  • 最后,取消 color_promptforce_color_prompt 变量的设置。

11. 终端标题的设置

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
  • 如果 $TERMxtermrxvt 系列终端,则设置终端标题为 user@host: dir

12. ls 命令的颜色支持

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
  • 如果 dircolors 命令可用,则根据 ~/.dircolors 文件或默认配置设置 LS_COLORS 变量。

  • 设置 lsgrepfgrepegrep 命令的别名为自动启用颜色输出。

13. 其他 ls 命令的别名

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
  • 定义一些常用的 ls 命令别名,方便使用。

14. 加载用户自定义的别名

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
  • 如果 ~/.bash_aliases 文件存在,则加载该文件中的别名定义。

15. 启用可编程补全功能

#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi
  • 这段代码被注释掉了,原本是用来启用 bash 的可编程补全功能的。如果需要启用,可以取消注释。

完整配置

# ~/.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

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

0

评论区