shell介绍及基本用法
8.1:shell介绍:
shell是一个命令解释器,提供用户和机器之间的交互,支持特定语法,支持逻辑判断、循环,并且每个用户都可以有自己的shell:
Centos默认的shell是bash(Bourne Agin Shell):其实为了纪念sh的创造者bourne这个用户:
常见的还有zsh(power - shell) ksh(korn - shell)这两种:支持的特性比较少:
8.2:命令历史:history
在系统中用户使用的命令都会保存下来,会保存在当前用户的家目录下:
history命令
语法: history [-c]
-c:=clear 清除内存中的命令,不能删除配置文件中的历史命令
[root@adai003 ~]# history 1 ls 2 ls /tmp/ 3 ls /boot/ 4 ls / 5 dhclient……[root@adai003 ~]# ls /root/.bash_history/root/.bash_history history的家目录
显示使用过的命令历史,默认保存1000条使用过的命令(注:此令需要是在正常关机操作情况下的处1000条命)!
history环境变量
变量HISTSIZE
[root@adai003 ~]# echo $HISTSIZE1000
该变量决定命令历史保存的命令的数目。
定义变量HISTSIZE
编辑其配置文件[root@adai003 ~]# vim /etc/profile ……HOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=1000……[root@adai003 ~]# echo $HISTSIZE1000[root@adai003 ~]# source /etc/profile[root@adai003 ~]# echo $HISTSIZE2000
搜索关键字"HIST"找到‘HISTSIZE=1000’,在此更改其数字,保存退出,然后执行命令‘source /etc/profile’刷新该配置文件才会生效。
更改history显示格式
[root@adai003 ~]# echo $HISTTIMEFORMAT[root@adai003 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "[root@adai003 ~]# echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S[root@adai003 ~]# history 1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 history
直接为‘HISTTIMEFORMAT’赋值即可,不过此时该格式只适用于当前终端。如果要其使用于所有用户,则需要将其写入history配置文件并刷新后生效。
[root@adai003 ~]# vim /etc/profile ……HOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=1000HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "……保存退出![root@adai003 ~]# source /etc/profile
命令历史永久保存
即,使命令历史记录只能写入不能被删除!
[root@adai003 ~]# chattr +a ~/.bash_history
使用文件特殊权限,为‘.bash_history’文件配置‘a’权限(只可追加,不可删除),限于正常关机操作。
‘!!’命令
[root@adai003 ~]# w……[root@adai003 ~]# !!w……
‘!’的用法:‘!n’(n代表数字),表示运行命令历史中的第n条命令;‘!word’,表示运行上一次以该word开头的命令。
eg:
[root@adai003 ~]# history 1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 history[root@adai003 ~]# !4echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S[root@adai003 ~]# !HISTHISTSIZE=1000
8.3 命令补全和别名
命令补全Tab
按一次tab可以补全一个命令或参数(需要安装包bash-completion,并重启系统);按两次tab可以显示以某字母开头的所有命令或文件名。
alias命令
语法: alias [命令别名]=[具体命令] 设置别名
取消别名:unalias [命令别名]
直接输入alias会显示系统所有的别名:
[root@adai003 ~]# aliasalias 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'[root@adai003 ~]#
系统别名存放在配置文件‘~/.bashrc’和‘ls /etc/profile.d/’下:
[root@adai003 ~]# cat !$cat .bashrc# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then . /etc/bashrcfi[root@adai003 ~]# ls /etc/profile.d/256term.csh colorgrep.sh lang.sh qt-graphicssystem.sh which2.sh256term.sh colorls.csh less.csh vim.cshbash_completion.sh colorls.sh less.sh vim.shcolorgrep.csh lang.csh qt-graphicssystem.csh which2.csh
8.4 通配符
通配符‘*’代表零个或多个任意字符
通配符‘?’代表一个任意字符
中括号‘[]’,“ls [0-9].txt”表示0-9区间内的任意.txt文件
花括号‘{}’,“ls {1,2,3}.txt”表示括号内任意.txt文件
输入输出重定向
“>,>>,<,2>,2>>”
‘>’:输出重定向
‘>>’:追加重定向
‘2>’:错误重定向
‘<’:输入重定向
使用‘>’命令时会将文件内原有内容删除。
[root@adai003 tmp]# echo adaixuelinux > 1.txt[root@adai003 tmp]# cat 1.txtadaixuelinux[root@adai003 tmp]# echo adaixu > 1.txt[root@adai003 tmp]# cat 1.txtadaixu#####################################[root@adai003 tmp]# echo adaixu >> 1.txt[root@adai003 tmp]# cat 1.txtadaixuadaixu#####################################[root@adai003 tmp]# lsaaa-bash: lsaaa: 未找到命令[root@adai003 tmp]# lsaaa 2> 2.txt[root@adai003 tmp]# cat 2.txt-bash: lsaaa: 未找到命令 输入重定向:必须定向到(<左边)一个命令下[root@adai003 tmp]# wc -l 1.txt “ wc -l”该命令用于查看文件行数2 1.txt
应用
[root@adai003 tmp]# ls {1,2}.txt aaaa.txt > 1.txt 2> 3.txt[root@adai003 tmp]# cat 1.txt1.txt2.txt[root@adai003 tmp]# cat 3.txtls: 无法访问aaaa.txt: 没有那个文件或目录
说明: 使用ls命令查看 {1,2}.txt aaaa.txt,1.txt和2.txt文件存在,可以使用ls查看,aaaa.txt不存在,使用ls查看会报错,‘> 1.txt 2> 3.txt’意思是将正确信息保存到1.txt,将错误信息保存到3.txt。
转载于:https://blog.51cto.com/yuanhaohao/1982188
shell介绍及基本用法相关推荐
- 8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向
8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向 8.1 shell介绍 Linux Shell基础 介绍shell的特性,用法. shell是 ...
- Linux Shell脚本入门教程系列之(九)Shell判断 if else 用法
本文是Linux Shell脚本系列教程的第(九)篇,更多shell教程请看:Linux Shell脚本系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.继上一篇之后,今天 ...
- Linux常用命令(本篇包括,Linux目录结构介绍、Linux Shell介绍、9个常见命令介绍、文件的概念、文件的操作(20个)、目录的操作、文件和目录的权限、文件压缩及解压缩)
Linux常用命令(本篇包括,Linux目录结构介绍.Linux Shell介绍.9个常见命令介绍.文件的概念.文件的操作(20个).目录的操作.文件和目录的权限.文件压缩及解压缩) ...
- Shell介绍及OhMyZsh
一,什么是shell 本部分来源于http://c.biancheng.net/cpp/shell/ Shell 诞生于 Unix,是与 Unix/Linux 交互的工具,单独地学习 Shell 是没 ...
- shell介绍,命令历史,命令补全和别名 ,通配符, 输入输出重定向
2019独角兽企业重金招聘Python工程师标准>>> shell介绍 shell是一个命令解释器,提供用户和机器之间的交互: 支持特定语法,比如逻辑判断.循环: 每个用户都可以有自 ...
- shell实例第22讲:shell中分隔符IFS用法
shell中分隔符IFS用法 1.什么是IFS? IFS在shell中是分隔符的意思,即IFS这个变量中存放了分隔符. 2.IFS是全局变量还是局部变量? (1)linux中变量分两种:全局变量env ...
- linux下的各种shell介绍(bash和dash转换)
在linux下编写shell脚本文件时,经常会看到很多人在文件的第一行中标注"#!/bin/bash".这句话的意思是告诉系统强制用bash,避免出现一些不兼容的问题. 那么,在l ...
- linux here用法,linux shell的here document用法(cat EOF)
什么是Here Document? Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下 cmd << delimiter Here Doc ...
- shell介绍 命令历史 命令补全和别名 通配符 输入输出重定向
8.1 shell介绍 shell 是一个命令解释器,提供用户和机器之间的交互 支持特定语法,比如逻辑判断,循环 每个用户都可以有自己特定的shell CentOS7默认shell 为bash(Bou ...
最新文章
- 【AI学院】新手如何学CV?老司机带学有三书籍《深度学习之图像识别》,赠书8本...
- 神州英才战略盈利竞争力总结
- android support design library eclipse,Eclipse下使用Android Design Support Library中的控件
- 03 - const static extern
- 理解TypeScript的泛型
- Visual Basic里与Print有关的函数
- python自学行吗-python能够自学吗
- maven依赖不到想要的jar包问题
- 亚马逊中国发布2015图书排行榜
- 鼠标自动不停地按右键
- mac下使用python3_mac 下使用python3运行ride
- 通过OpenSSL解析X509证书基本项
- 自建题库刷题小程序,添加更丰富的练习设置,模糊选项、自动切题、夜间模式、答题音效、答对移除错题、练习完交卷后统计正确率
- Docker容器技术(三)
- 牛奶食疗可治多种疾病(图)
- macOS中修改应用或文件图标
- 【破解利器】PE工具篇(PE编辑,查看等操作)
- mldonkey设置!看图(转载)
- Ctdb Rados(二):多场景断网高可用
- 云原生架构下复杂工作负载混合调度的思考与实践