Bash特性

Posted 少年已不年少

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bash特性相关的知识,希望对你有一定的参考价值。

Bash特性

基础语法

命令      [选项]       [参数]
Command [option] [argument]
在语法中,中括号是可无可有的意思

列如:list
ls:查看当前所在工作目录下的文件和目录
[root@Quanyi <sub>]# ls
anaconda-ks.cfg

-a:查看所有文件包括隐藏文件
[root@quanyi </sub>]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .swp
.. .bash_history .bash_profile .cshrc .tcshrc

-1:查看文件的详细属性信息
[root@Quanyi ~]# ls -l
total 4
-rw-------. 1 root root 1450 Mar 15 12:00 anaconda-ks.cfg

命令补全

Tab键

默认可以补全,命令跟参数

Bash快捷键

Ctrl + l:清屏
Ctrl + C:终止命令执行
Ctrl + e:end 将光标快速移动到行末
ctrl + a:ahead 将光标快速移动到行首
Ctrl + w:以空格为分割符,删除光标前面到空格之间的内容
Ctrl + k:删除光标之后所有的内容
Ctrl + u:删除光标之前所有的内容
Ctrl + d:退出当前用户的登录
ctrl + r:搜索执行过的历史命令
ctrl + 左右:按照单词移动光标
Esc + .:复制上一条命令,最后一个空格后面的内容

注释:是给其他人看的,计算机不认识

历史命令

history
-c:clear 清楚历史命令

-d:delete 删除指定编号的历史命令
[root@Quanyi <sub>]# history
1 ls
2 history
3 ls -a
4 ls -l
5 quanyi
6 history
[root@Quanyi </sub>]# history -d 5
[root@Quanyi <sub>]# history
1 ls
2 history
3 ls -a
4 ls -l
5 history
6 history -d 5
7 history

-w:write 保存历史命令到一个文件中(将历史命令保存到家目录下的.bash_history隐藏文件中)
[root@Quanyi </sub>]# cat .bash_history
ls
history
ls -a
ls -l
history
history -d 5
history
history -w
------------------------------------------------------------------------------------
+命令的一部分:执行上一条命令,含义该命令一部分的命令内容(常用)
[root@Quanyi <sub>]# !his
history -w

!!:执行上一条命令
[root@Quanyi </sub>]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .swp
.. .bash_history .bash_profile .cshrc .tcshrc
[root@Quanyi <sub>]# !!
ls -a
. anaconda-ks.cfg .bash_logout .bashrc .swp
.. .bash_history .bash_profile .cshrc .tcshrc

+数字:执行历史命令中编号所在命令内容
[root@Quanyi </sub>]# !1
ls
anaconda-ks.cfg

命令的别名

# 查看系统内置的别名都有哪些
alias

[root@Quanyi <sub>]# alias
alias cp=cp -i
alias egrep=egrep --color=auto
alias fgrep=fgrep --color=auto
alias grep=grep --color=auto
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 别名="完整的命令"

[root@Quanyi </sub>]# alias quanyiqli=ls
[root@Quanyi <sub>]# quanyi
-bash: quanyi: command not found
[root@Quanyi </sub>]# ls
anaconda-ks.cfg
[root@Quanyi <sub>]# quanyiqli
anaconda-ks.cfg

# 取消别名
unalias ls
[root@Quanyi </sub>]# unalias quanyiqli
[root@Quanyi ~]# qaunyiqli
-bash: qaunyiqli: command not found

Linux系统中帮助文档

man 命令
列如:man ls #帮助

NAME
ls - list directory contents

SYNOPSIS
ls [OPTION]... [FILE]...

DESCRIPTION
List information about the FILEs (the current directory by default). Sort
entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
do not ignore entries starting with .

-A, --almost-all
do not list implied . and ..

--author
with -l, print the author of each file

-b, --escape
Manual page ls(1) line 1 (press h for help or q to quit)

命令 --help

例如:history --help
[root@Quanyi ~]# history --help
-bash: history: --: invalid option
history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]

以上是关于Bash特性的主要内容,如果未能解决你的问题,请参考以下文章

bash基本特性

bash特性

Bash基础特性

bash特性小结&bash变量

bash特性

shell编程bash特性