Bash 技巧
Bash 使用技巧
Bash 是 GNU 项目的 Bourne Again SHell,
具有交互式命令行编辑、支持它的体系结构上的作业控制、类似 csh 的功能
- Bash 是免费软件,根据 GNU 通用公共许可证第 3 版的条款分发
- Bash 是 GNU 操作系统的 shell 或命令语言解释器
如果不了解 shell 或命令语言解释器 建议百度一下了解个大概,以下不包括 shell 脚本相关内容
主要是对 bash 的定制化或配置有关
常用操作
首先需要知道的基本内容,确认版本号
-
查看 bash 版本
bash --version Bash 是 GNU / Linux 系统上的标准 shell,其中大部分使用的是 bash-4.4 或 bash-5.0
bash 的按键绑定
快捷键 | 描述 |
---|---|
Ctrl+H | 删除光标前的一个字符 |
Ctrl+U | 删除光标前到行首的字符 |
Ctrl+C | 终止一个正在运行的程序 |
Ctrl+D | 终止输入,如果你在使用 shell 则退出 |
Ctrl+Z | 通过将程序移动到后台来暂停程序,相关操作命令 jobs, fg, bg, kill |
Ctrl+S | 停止屏幕输出 |
Ctrl+Q | 激活屏幕输出 |
Ctrl+Alt+Del | 重启或关闭系统 |
Up 向上方向键 ↑ | 在 bash 中查看命令历史 |
Ctrl-R | 在 bash 的增量命令历史中搜索 |
Tab | 自动补全,如果只是想输入 Tab 可以先按 Ctrl+V |
常用判断命令
-
pwd: 显示当前/工作目录
-
whoami: 显示当前的用户名
-
id: 显示当前用户的身份
-
file 文件: 查看文件信息
-
查命令位置
- type -p command
- which command
常用的权限设置
- chmod 600 foo: 使其他人无法读写现有 "foo" 文件,并且所有人都无法执行该文件
- chmod 644 foo: 使其他人对现有 "foo" 文件可读但不可写,并且所有人都无法执行该文件
- chmod 755 foo: 使其他人对 "foo" 文件可读而不可写,并且所有人都能执行该文件
重要的环境变量
-
$LANG
: 默认的语言环境,值的格式一般为xx_YY.ZZZZ
- xx: ISO 639 语言码
- YY: ISO 3166 国家码
- ZZZZ: 编码格式
常见设置
语言环境 描述 zh_CN.UTF-8 汉语(中国) zh_TW.UTF-8 汉语(中国台湾) en_US.UTF-8 英语(美国) en_GB.UTF-8 英语(英国) fr_FR.UTF-8 法语(法国) de_DE.UTF-8 德语(德国) it_IT.UTF-8 意大利语(意大利) es_ES.UTF-8 西班牙语(西班牙) sv_SE.UTF-8 瑞典语(瑞典) pt_BR.UTF-8 葡萄牙语(巴西) ru_RU.UTF-8 俄语(俄国) ja_JP.UTF-8 日语(日本) ko_KR.UTF-8 韩语(韩国) -
$PATH
: 在 Shell 里输入命令的时候,会在此变量所包含的目录列表里进行搜索 -
$HOME
: 指向用户目录,符号~
表示当前用户目录关于 sudo 执行时,虽然用 root 权限,但此时 $HOME 并不是
/root/
目录,可以通过 sudo -H command 使用 root 权限同时 $HOME 值为 "/root/"
常用管道重定向
命令常见用法 | 说明 |
---|---|
command & | 在子 shell 的后台 中执行 command |
command1 | command2 | 通过管道将 command1 的标准输出作为 command2 的标准输入(并行执行) |
command1 2>&1 | command2 | 通过管道将 command1 的标准输出和标准错误作为 command2 的标准输入(并行执行) |
command1 ; command2 | 依次执行 command1 和 command2 |
command1 && command2 | 如果执行 command1 成功,按顺序执行 command2(如果 command1 和 command2 都执行成功,返回 success ) |
command1 || command2 | 如果执行 command1 不成功,按顺序执行 command2(如果 command1 或 command2 执行成功,返回 success ) |
command > foo | 将 command 的标准输出重定向到文件 foo(覆盖) |
command 2> foo | 将 command 的标准错误重定向到文件 foo(覆盖) |
command >> foo | 将 command 的标准输出重定向到文件 foo(附加) |
command 2>> foo | 将 command 的标准错误重定向到文件 foo(附加) |
command > foo 2>&1 | 将 command 的标准输出和标准错误重定向到文件 foo |
command < foo | 将 command 的标准输入重定向到文件 foo |
command << delimiter | 将 command 的标准输入重定向到下面的命令行,直到遇到 "delimiter"(here document) |
command <<- delimiter | 将 command 的标准输入重定向到下面的命令行,直到遇到 "delimiter"(here document,命令行中开头的制表符会被忽略) |
- delimiter 是一个标记符,原则上是随意,但一般使用
EOF
cat << EOF Hello, my friend. Bye. EOF
个性化配置
其配置文件 .bashrc
: 这个文件主要保存个人的一些个性化设置
# ~/.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 case $- in *i*) ;; *) return;; esac # don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth # 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 # If set, the pattern "**" used in a pathname expansion context will # match all files and zero or more directories and subdirectories. #shopt -s globstar # 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|*-256color) 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 # colored GCC warnings and errors #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' # some more ls aliases #alias ll='ls -l' #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 ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fi fi
启用其中的 force_color_prompt=yes
才能进行颜色渲染
有两种方法使得 .bashrc 文件生效
-
每次修改 .bashrc 后,使用 source ~/.bashrc 使之生效
-
也可以在 .bash_profile 文件中显式调用 .bashrc
登陆 Linux 系统启动 bash 时首先会去读取 ~/.bash_profile 文件,就会使得 ~/.bashrc 马上生效
其中变量设置 shell 内容 -> 更多
字体配置
在文件 /etc/default/console-setup
通过命令
sudo dpkg-reconfigure console-setup
- 要显示你的控制台字体,使用
showconsolefont
命令
文本工具
这里有一些在类 Unix 系统中经常使用到的标准文本处理工具
基础文本处理
- cat: 连接文件并输出全部的内容
- tac: 连接文件并反向输出
- cut: 选择行的一部分并输出
- head: 输出文件的开头
- tail: 输出文件的末尾
- sort: 对文本文件的行进行排序
- uniq: 从已排序的文件中移除相同的行
- tr: 转换或删除字符
- diff: 对文件的行进行对比
使用基础正则表达式 BRE
- ed: 是一个原始行编辑器
- sed: 是一个流编辑器
- grep: 匹配满足 pattern 的文本
- vim: 是一个屏幕编辑器
- emacs: 是一个屏幕编辑器,后期版本支持部分 BRE
使用扩展的正则表达式 ERE
- awk: 进行简单的文本处理
- gawk: 支持更多高级功能
- egrep: 匹配满足多个 pattern 的文本
- tcl: 可以进行任何你想得到的文本处理,经常与 tk 一起使用
- perl: 可以进行任何你想得到的文本处理
- pcregrep: 可以匹配满足 Perl 兼容正则表达式 PCRE 模式的文本
三剑客
其中使用频率最高的是:grep, sed, awk
- grep: 适合单纯的查找或匹配文本
- sed: 适合编辑匹配到的文本
- awk: 适合用来从这种类型的文件中提取数据,或格式化文本
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了