Bash和Zsh的配置及alias别名命令

写这篇文章的原因是,今天(2022年11月12日)遇到了gitlab-runner不能正确运行的问题,当初步解决问题后,又遇到了新的问题。在gitlab-runner运行时,gitlab网站上的popline详细执行情况显示,总是出现命令hostname找不到一行错误信息,于是查询后发现在ArchLinux中是以命令hostnamectl来代替了别的版本中的hostname命令,于是想定义一个命令别名hostname来定义hostnamectl hostname,但是根据网上教程设置了~/.bashrc~/.bash_profile 但是总是不能正常工作。虽然最后没有解决gitlab中的popline问题,但是通过这个事情,搞清楚了命令别名的方法。

sh和bash

  1. sh全称为Bourne Shell,来自于1977年底的Unix v7版。二进制文件位置为/bin/sh

  2. bash全称为Bourne-Again Shell,是在1989年对sh的重写版,替代了sh。二进制文件位置为/bin/bash。

  3. 现在的Linux系统以及macOS系统的shell都默认使用bash。

zsh

Zsh即Z shell,是一款可用作交互式登录的shell及脚本编写的命令解释器。Zsh对Bourne shell做出了大量改进,同时加入了Bash、ksh及tcsh的某些功能。

所以说Zsh也是一个Shell,就是会比bash能好用很多。所以这也是2019年起,macOS的默认Shell从Bash改为Zsh的原因?

Zsh好在哪里?我认为提高了交互的效率,以及丰富的插件。比如:

  1. 强大的补全功能

    • 使用tab键可以补全命令,补全命令后的参数等,还自带命令的解释
    • kill命令还可以补全进程pid
  2. 跳转不用cd,直接输入路径

  3. 历史命令,敲出命令的前几个字母,按上下箭头就可以查看这个前缀的历史命令

  4. 完善的插件机制以及丰富的生态,和oh my zsh一起食用更佳

查看本地Shell的命令

  1. 查看当前Linux的全部Shell:cat /etc/shells

  2. 查看当前使用的Shell:echo $SHELL或者echo $0

  3. 切换默认的Shell:chsh -s [shell的二进制文件路径],之后要重新登陆Shell才能生效哈。例如:

    • 切换为Bash: chsh -s /bin/bash
    • 切换为Zsh:chsh -s /bin/zsh

Shell的各种配置文件

无论是Bash还是Zsh,都是有各自的配置文件来完成自己的工作的。这些配置文件都是隐藏文件,也就是说,用户不需要知道这些配置。但是在这里也要给大家总结一下都有哪些配置文件,也许以后用的上呢。

Bash的配置文件

文件 解释
/etc/profile 为系统的每个用户设置环境信息,当用户第一次登录时会执行该文件里的命令。默认会直接调用/etc/bashrc。该文件的改动需要重启才能生效。
/etc/bashrc 为每一个运行bash shell的用户执行此文件。当bash shell被打开时,会读取并执行该文件中的命令。所以修改该文件后,重新打开Shell即可生效。
~/.bash_profile 和/etc/profile类似,但是只对当前用户生效。
~/.bashrc 和/etc/bashrc类似,但是只对当前用户生效。
~/.bash_logout 当每次退出bash shell时,执行该文件。
~/.bash_history 保存了历史命令。在Shell为Bash时,每次敲击命令时,都会保存在这个文件里

Zsh的配置文件

文件 解释
.zshenv 存放的环境变量配置项在任何场景下都能被读取,这里通常把$PATH等变量写在这里,这样无论是在交互shell,或者运行程序都会读取此文件。个人理解对标Bash的profile。
.zshrc 在交互式shell中会读取并执行该文件,包含设置别名、函数、选项、键绑定等命令。对标bashrc。
.zlogin 在login shell的时候读取。
.zprofile 和.zlogin类似,但是是在.zshrc之前加载。
.zlogout 退出终端的时候读取,用于做一些清理工作。对标bash_logout。
.zsh_history 保存了历史命令。在Shell为Zsh时,每次敲击命令时,都会保存在这个文件里。

加载顺序:zshenv、zprofile、zshrc、zlogin

alias问题的解决

alias命令是linux内置的命令,但是当我设置好别名后发现不能执行,究其原因是我所使用的是zsh,并且设置为了默认shell,这样的修改bash的配置文件不起作用就显而易见了!如果要命名一个命令的别名,需要修改zsh的配置文件,比如当前thinkpad-t490上的配置文件为

~/.zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
ZSH=/usr/share/oh-my-zsh/

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
#ZSH_THEME="random"
#ZSH_THEME="robbyrussell"
ZSH_THEME="steeef"


# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled # disable automatic updates
# zstyle ':omz:update' mode auto # update automatically without asking
# zstyle ':omz:update' mode reminder # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)


# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias hostname="hostnamectl hostname"

ZSH_CACHE_DIR=$HOME/.cache/oh-my-zsh
if [[ ! -d $ZSH_CACHE_DIR ]]; then
mkdir $ZSH_CACHE_DIR
fi

source $ZSH/oh-my-zsh.sh