02.Linux System Bash initial knowledge

Source: Internet
Author: User

1.Linux System Terminal Overview

Server Terminal switch: Ctrl + Alt + F1 ... F6
Virtual Machine Terminal switch: Ctrl +shift +alt +f1 ... F6
The virtual terminal is generated by the mingetty program

//物理机执行为本地终端[[email protected] ~]# tty/dev/tty1//通过网络使用模拟终端远程连接Linux, 日常运维中就是这种方式。[[email protected] ~]# tty/dev/pts/1
2.Linux System Bash Management

In a nutshell, the shell is the user interface of the system and provides an interface for users to interact with the kernel. It receives the command entered by the user and feeds it into the kernel to execute.

The shell is actually a command interpreter that interprets the commands entered by the user and sends them to the kernel.
Linux also provides a visual command input interface like MicrosoftWindows X Window's graphical user interface it provides many desktop environment systems that operate like windows, have windows, icons and menus, and all management is controlled by the mouse. Gnome.

Linux, the shell is also available in several different versions. There are mainly the following versions of the shell:

BASH Shell: A Unix shell written for the GNU program, the default shell for many Linux distributions
C Shell: Is the BSD version of Sun's shell.
Z Shell: It integrates the important features of bash and ksh, while adding its own unique features.

There are a lot of traditional Unix shells, such as tcsh, CSH, Ash, BSH, Ksh, and so on, shell scripts are roughly the same, and when you learn a shell, the other shells quickly get started, most of the time, a shell script It can often be used on many kinds of shells.

2.1.Bash Features: Command completion

Through the upper and lower arrow keys to adjust the past executed Linux commands;
Commands or parameters only need to input the first few can be used to complete the TAB key;

tabKey can implement command completion, path completion and command implementation of the same way, we often use the tab completion, reduce the execution of commands and path error rate.

//查看ip时忘记具体了命令[[email protected] ~]# ifcon//按下tab键会自动补全[[email protected] ~]# ifconfig//按一下tab键没有反应, 按两下tab键列出所有if开头的命令[[email protected] ~]# ifif         ifconfig   ifenslave  ifrenameifcfg      ifdown     ifnames    ifup//linux路径较深, 经常使用tab键进行补全, 如果路径出错是没有办法补全[[email protected] ~]# ls /etc/sysconfig/network-scripts/
2.2.Bash Features: Command shortcut keys

The use of terminal shortcuts can help us greatly improve our efficiency.

Ctrl + A    //光标跳转至正在输入的命令行的首部Ctrl + E    //光标跳转至正在输入的命令行的尾部Ctrl + C    //终止前台运行的程序Ctrl + D    //在shell中,ctrl-d表示推出当前shell。Ctrl + Z    //将任务暂停,挂至后台Ctrl + L    //清屏,和clear命令等效。Ctrl + K    //删除从光标到行末的所有字符Ctrl + U    //删除从光标到行首的所有字符Ctrl + R    //搜索历史命令, 利用关键字//在命令行前加面加 "#" 则该命令不会被执行

HistoryView the history of the system command and trace what happened before

Command options
-W Save command history to history file
-C Clears the command history, no case files
-D Delete the nth row of the command history

//使用双 !! 可执行上一条命令[[email protected] ~]# lslocalhost.com[[email protected] ~]# !!lslocalhost.com//输入!6, 执行history命令历史中第 6 行命令[[email protected] ~]# !6touch localhost.com//使用!cat, 调用history命令历史最近一次执行过的cat命令[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0[[email protected] ~]# !catcat /etc/sysconfig/network-scripts/ifcfg-eth0执行当前命令, 调用上一条命令最后一个参数[[email protected] ~]# ls /etc/passwd//调用上一条命令的最后一行, 按下ESC松开, 然后按下 "."[[email protected] ~]# cat[[email protected] ~]# cat /etc/passwd//第二种方式, 输入!$[[email protected] ~]# ls !$ls /etc/passwd/etc/passwd
2.3.Bash Features: Command flow

When we execute a command, the entire command execution process is as follows:
1. Determine if the command is executed through an absolute path
2. Determine if the command has an alias
3. Determine whether the user is entering internal or external commands
4. Internal command executes directly, external command detects if cache exists
5. Detection path, there is execution, no error

Internal command: The command that comes with the shell program.
External command: The executable program under a path of the system path variable.

//1.定义临时别名, if1为查看eth0网卡别名[[email protected] ~]# alias if0=‘ifconfig eth0‘[[email protected] ~]# if0//如果定义命令本身, 会执行什么?[[email protected] ~]# alias ifconfig=‘ifconfig eth0‘    //绝对路径执行, 调用命令本身    [[email protected] ~]# /sbin/ifconfig    //通过\转义字符, 调用命令本身    
2.4.Bash Features: Command flow

When we execute a command, the entire command execution process is as follows:
1. Determine if the command is executed through an absolute path
2. Determine if the command has an alias
3. Determine whether the user is entering internal or external commands
4. Internal command executes directly, external command detects if cache exists
5. Detection path, there is execution, no error

Internal command: The command that comes with the shell program.
External command: The executable program under a path of the system path variable.

Use the type command to detect whether the commands entered by the user are internal or external commands.

//cd命令属于shell内部命令[[email protected] ~]# type -a cdcd is a shell builtincd is /usr/bin/cd//ping属于外部命令, 同时会打印当前命令路径[[email protected] ~]# type -a  pingping is /bin/ping

The path variable defines the location where the external commands that bash executes are stored, and bash scans the paths individually.

//echo所见即所得[[email protected] ~]# echo "123"123[[email protected] ~]# echo -e "123 \n456"123456//打印当前环境变量目录[[email protected] ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin//PATH由多个路径组成,每个路径值之间用冒号间隔,对这些路径的增加和删除操作都将影响到Bash解释器对Linux命令的查找。//修改PATH变量[[email protected] ~]# PATH=/soft/bin:$PATH//写入/etc/profile配置文件永久生效echo ‘export PATH=/soft/bin:$PATH‘ >> /etc/profile

In fact, external commands that are executed are saved in the memory cache, and when the same command is executed again, the cache is tuned for execution, which means that the path is not searched.

//表缓存命令所在位置[[email protected] ~]# hashhits    command   1    /usr/bin/tty   3    /sbin/ifconfig//已缓存命令,如果移动位置会导致无法找到该命令[[email protected] ~]# mv /sbin/ifconfig /bin/[[email protected] ~]# ifconfig-bash: /sbin/ifconfig: No such file or directory//删除缓存过的ifconfig命令, 即可执行[[email protected] ~]# hash -d ifconfig[[email protected] ~]# ifconfig//当然可以情况缓存表[[email protected] ~]# hash -r//命令缓存hash需要注意如下情况:1.如果执行外部命令1次就会对该命令进行缓存2.如果将命令移动了位置    a.使用绝对路径执行    b.删除hash表的缓存指令

In summary, when we execute a ping command, the entire command executes the following process steps:

1. Check if the execution is absolute path execution
2. Check if the ping command has an alias
3. Check whether the ping is an internal or external command
4. If the internal instruction is executed directly, if it is an external command
5. Detect if the command has a cache, and if not, find the PATH variable
6. Check your path path until you find the command and execute
7. Returns an error if the command is not found. Command not Found

2.5.Bash Features: Path expansion

Linux ShellThe following path expands with curly braces including, comma-delimited, so that the contents of the curly braces are expanded to form a list.

[[email protected] ~]# mkdir /tmp/zz/a/b /tmp/yy/a/b -pvmkdir: created directory `/tmp/zz‘mkdir: created directory `/tmp/zz/a‘mkdir: created directory `/tmp/zz/a/b‘mkdir: created directory `/tmp/yy‘mkdir: created directory `/tmp/yy/a‘mkdir: created directory `/tmp/yy/a/b‘//删除目录[[email protected] ~]# rm -rf /tmp/{zz,yy}//通过路径展开方式创建目录[[email protected] ~]# mkdir /tmp/{zz,yy}/a/b -pvmkdir: created directory `/tmp/zz‘mkdir: created directory `/tmp/zz/a‘mkdir: created directory `/tmp/zz/a/b‘mkdir: created directory `/tmp/yy‘mkdir: created directory `/tmp/yy/a‘mkdir: created directory `/tmp/yy/a/b‘思考:在/tmp 路径下创建以下目录 etc/init.detc/sysconfigusr/libusr/binusr/includevar/spoolvar/run proc sysbin
2.6.Bash feature: Escape character

The shell interpreter provides a very rich escape character for character processing and command substitution.

Class 4 Common escape characters

backslash (\): Causes a variable following the backslash to become a string.
Single quotation mark ('): Escapes all of its variables as pure strings.
Double quotation mark (""): retains the variable attribute in it and does not escape processing.
Anti-quote ('): Returns the result after executing the command.

//将特殊字符转换为正常字符[[email protected] ~]# echo "shoping is $500"shoping is 00[[email protected] ~]# echo "shoping is \$500"shoping is $500//转义其中特殊字符为普通字符串[[email protected] ~]# echo ‘shoping is $500‘shoping is $500//使用$()实现命令替换[[email protected] ~]# echo "The Directory is $(pwd)"The Directory is /root//使用``实现命令替换[[email protected] ~]# echo "The Directory is `pwd`"The Directory is /root//转义其中所有的变量为单纯的字符串[[email protected] ~]# touch file-`date +%F-%H-%S`[[email protected] ~]# lsfile-2017-12-03-04-18[[email protected] ~]# touch ‘file-`date +%F-%H-%S`‘[[email protected] ~]# lsfile-2017-12-03-04-18  file-`date +%F-%H-%S`
2.7.Bash Features: Getting Help

1. Get help with the man command

手册的常见级别: 1(普通用户命令手册) , 5(配置文件手册), 8(管理员命令手册)g : 回到手册的顶部G : 去到文档的底部/ : 输入要搜索的关键字,进行文档搜索q: 退出文档回车: 往下滚动一行空格键: 往下翻一页man -a passwdman  5 passwdman  1 passwdman -k passw  找到所有的包含passwd关键字的手册并且列出来

2. Get help with info or pinfo

首页寻找Textinfo文档,如果没有,强制打开对应的Man文档    与vi风格类似,快捷键:        空格:翻页        HOME: 返回页面顶部        END:返回页面底部        上箭头: 光标上移动一行        下箭头: 光标下移动以行        左箭头: 返回上一章节        右箭头: 浏览光标所在章节        d: 回到手册首页        q: 退出手册强制打开指定级别的man文档$ pinfo -m 5 passwd

02.Linux System Bash initial knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.