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: 适合用来从这种类型的文件中提取数据,或格式化文本
Bash 技巧的更多相关文章
- Bash技巧:使用 set 内置命令帮助调试 shell 脚本
Bash技巧:使用 set 内置命令帮助调试 shell 脚本 霜鱼片发布于 2020-02-03 在 bash 中,可以使用 set 内置命令设置和查看 shell 的属性.这些属性会影响 sh ...
- Linux教程:Bash技巧,让chmod只修改某个目录下文件夹或者文件的权限
用Linux的人都知道chmod -R 可以修改一个文件夹下的所有文件和文件夹的权限,但是这也存在着一个很大的弊端,那就是修改的是所有的文件和文件夹的权限,如果我只想修改文件或文件夹的权限该怎么办呢? ...
- Linux进阶:让效率翻倍的Bash技巧(一)
http://blog.tpircsboy.com/tech/bash-skills-part1/
- 10 个提升效率的Linux小技巧
您是否曾经惊讶于看到某人在 UNIX 中非常快速地工作,触发命令并快速地执行操作?是的,我碰到过几次,并且我一直都在向那些超级巨星开发者学习.在本文中,我想分享一些 UNIX 命令实践,这些实践是我在 ...
- python环境准备
一.环境准备. 1.安装python3.5.2(勾选环境变量),python2.7.12 2.设置环境变量 (要求命令行输入python,进入python2命令行,打python3时,进入python ...
- Docker-核心笔记(含Dockerfile,Compose)
Docker-核心笔记(含Dockerfile,Compose) 2017/03 Chenxin 参考 https://yeasy.gitbooks.io/docker_practice Docker ...
- Kubernetes 编排神器之 Helm
什么是Kubernetes Helm?为什么要使用Helm? 前言 编写一堆Kubernetes配置文件是一件很麻烦的事情.对于一些容器,我们可能需要10多个yaml文件.维护它们是一个问题,而且在不 ...
- bash小技巧
Linux 下shell基本上默认是 bash, 下面是我总结的一些技巧. & 后台运行程序 ,注意退出当前shell后 程序也会退出() 使用子shell, 比如 (cd ../../ ...
- Bash 使用技巧
Bash 是我们经常与之打交道的 Shell 程序,本文针对其使用技巧进行了搜罗.相信在你看过这些内容之后,定会在 Bash 的世界里游刃有余. 从历史中执行命令 有时候,我们需要在 Bash 中重复 ...
- 【转】Linux 技巧: Bash 参数和参数扩展
重点看下清单7 现在,很多 Linux® 和 UNIX® 系统上都有 bash shell,它是 Linux 上常见的默认 shell.通过本文,您将了解到如何在 bash 脚本中处理参数和选项,以及 ...
随机推荐
- C# Datagridview combox列 初始化颜色
DataGridView 初始化完成后,在combox里显示颜色,如这样: DataGridView 注册 cellPainting事件: private void m_dataGridView_Ce ...
- MR+meta分析的摘录
第四篇公众号:来自微信 天桥下的卖艺者 零基础说科研,仅为个人学习用,如有侵权,可以删除 吸烟没什么创意,唯一的创意就是加入了MR和meta分析,作者显示介绍吸烟与多种疾病之间的因果关系扔不明确, 第 ...
- 如何从零开始实现TDOA技术的 UWB 精确定位系统(6)
这是一个系列文章<如何从零开始实现TDOA技术的 UWB 精确定位系统>第6部分. 重要提示(劝退说明): Q:做这个定位系统需要基础么? A:文章不是写给小白看的,需要有电子技术和软件编 ...
- 【Effective C++】设计与声明——reference篇
以pass-by-reference-to-const替换pass-by-value 缺省情况下C++以by value方式传递对象至(或来自)函数.除非你另外指定,否则函数参数都是以实际实参的复件( ...
- C++笔记(9) 异常
程序有时会遇到运行阶段错误,导致程序无法正常走下去.对于这种问题,处理方法主要有: 1.调用abort() Abort()函数原型位于头文件cstdlib,其典型实现是向标准错误流(即cerr使用的错 ...
- 机器学习笔记(2): Logistic 回归
Logistic 回归是线性回归中一个很重要的部分. Logistic 函数: \[\sigma(x) = \frac {L} {1 + \exp(-k(x - x_0))} \] 其中: \(L\) ...
- CF1753
CF1753 成功因为虚拟机炸了,重新写一遍此文. 都是没有保存的错. A. Make Nonzero Sum 由于 Note that it is not required to minimize ...
- xv6 的锁机制
LOCK 公众号:Rand_cs 锁,大家应该很熟悉了,用来避免竞争,实现同步.本文以 xv6 为例来讲解锁本身是怎么实现的,废话不多说,先来看一些需要了解的概念: 一些概念 公共资源:顾名思义就是被 ...
- kettle从入门到精通 第三十九课 kettle 增量同步(日级)
1.上一课我们学习了在数据量大的情况下的分页全量同步示例,本次我们一起学习下kettle 增量全量同步.有些业务场景不需要实时数据,比如统计t-1日的销售业绩情况等. 2.kettle增量全量同步 ...
- SpringMVC 工作原理?
a.客户端发送请求到 DispatcherServlet b.DispatcherServlet 查询 handlerMapping 找到处理请求的 Controller c.Controller 调 ...