【例1】显示当前使用的shellmysql
[root@centos7 ~]# echo ${SHELL} /bin/bash
【例2】显示当前系统使用的全部shellsql
[root@centos7 ~]#cat /etc/shells /bin/sh /bin/bash /usr/bin/sh /usr/bin/bash /bin/tcsh /bin/csh
一、type命令:判断内部或外部
格式:type COMMANDshell
【例3】type查看内部显示的结果centos
[root@centos7 ~]#type cd cd is a shell builtin
【例4】type查看外部命令显示的结果bash
[root@centos7 ~]#type top top is /usr/bin/top
二、alias命令:查看或设置命令别名
格式:alias NAME=ʼVALUEʼ
取消别名:unalias [-a] NAME
-a:表示取消全部别名。
NAME:表示自定义的命令别名名称。
若是别名同原命令同名,要执行原命令时,可以使用方式有多种:
\命令名称
ʻ命令名称ʼ
/PATH/COMMAND
"【例7】不使用ls的别名
使用:命令前加斜杠 \
命令加引号 ''
命令写全路径"服务器
【例5】设置命令别名为bkmysql,实现拷贝/etc/my.cnf为/home/back/my.cnf.bak,设置后查看别名session
[root@centos7 ~]#alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak' [root@centos7 ~]#alias alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak' 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'
【例6】把上例中别名设置写在配置⽂件⾥,永久⽣效
有两个文件能够写,~/.bashrc⽂件仅当对当前⽤户,/etc/bashrc⽂件是对全部⽤户有效。ssh
[root@centos7 ~]#echo "alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak'" >>~/.bashrc [root@centos7 ~]#tail -1 ~/.bashrc alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak'
一、date命令:显示日期和时间 ide
格式:
date [OPTION]... [+FORMAT]
date 选项 参数/格式
clock -w (软件时间同步到硬件时间)
clock -s (硬件时间同步到软件时间) 学习
经常使用选项:
-s:指定时间和日期
[例8]设置日期为
[root@centos7 ~]#date -s'20190318 17:07:50' Mon Mar 18 17:07:50 CST 2019
显示当前时间
[root@centos7 ~]#date
Mon Mar 18 17:18:13 CST 2019
2.cal 命令:显示日历
[root@centos7 ~]#cal March 2019 Su Mo Tu We Th Fr Sa 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
3.shutdown命令:关机或重启
经常使用选项
-h :至关于halt关机
4.用户信息查看命令
Whoami命令 Who命令 W命令
均可以查看用户但稍有区别。
[root@centos7 ~]#whoami root [[17:23:59 root@centos7 ~]#who root :0 2019-03-18 23:08 (:0) root pts/0 2019-03-18 23:10 (:0) root pts/1 2019-03-18 23:13 (:0) root pts/2 2019-03-19 00:20 (192.168.36.1) [[17:24:04 root@centos7 ~]#w 17:24:06 up 2:18, 4 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root :0 :0 23:08 ?xdm? 2:03 0.55s /usr/libexec/gnome-session-binary --se root pts/0 :0 23:10 ? 0.05s 0.05s bash root pts/1 :0 23:13 ? 0.04s 0.04s bash root pts/2 192.168.36.1 00:20 ? 0.38s 0.07s w
[root@centos7 ~]#echo $PATH .:/data/testshell:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]#echo 'abc' abc
[root@centos7 ~]#echo -n "abc" abc[17:39:15 root@centos7 ~]#
扩展命令:$() ``(在Tab键上面)
[root@centos7 ~]#echo "i am 'whoami'" i am 'whoami' [root@centos7 ~]#echo "i am `whoami`" i am root [root@centos7 ~]#echo "i am $(whoami)" i am root
七、大括号扩展:{}
【例19】数字、字符扩展
[root@centos7 ~]#echo {1..10} 1 2 3 4 5 6 7 8 9 10
[root@centos7 ~]#echo {a..g} a b c d e f g
8.连按两次可补全命令或显示可操做目录/文件
9命令历史:
重复前一个命令:
十、调用上一次命令最后一个参数:有多种方法
1.输入:!$
2.按Esc键松开后,再按.(点号,同时按)
【例21】调用上一次命令最后一个参数
[root@centos7 data]#ll /etc/ssh//sshd_config -rw-------. 1 root root 3907 Apr 11 2018 /etc/ssh//sshd_config [root@centos7 data]#file !$ file /etc/ssh//sshd_config /etc/ssh//sshd_config: ASCII text
十一、命令历史环境变量:
系统默认的环境变量都使用大写字母,显需环境变量的内容,则须要使用echo$环境变量名称的形式查看其值。而要想修改或设置环境变量,有两种方法,一种是可直接在命令行使用export命令方式设置环境变量,则当即生效,退出shell登陆或服务器重启失效,另外一种是把export命令设置环境变量代码写到配置文件里,重读配置文件则永久生效。重读配置文件生效有两种方法:一种是使用source命令重读配置文件生效,另使种使号。
点号。
HISTISIZE:命令历史记录条数
查看命令历史条数
[root@centos7 data]#echo $HISTSIZE 1000
【例23】临时修改环境变量历史记录条数
[root@centos7 data]#export HISTSIZE=5000 [root@centos7 data]#echo $HISTSIZE 5000
【例24】永久修改环境变量历史记录条数
[root@centos7 data]#echo "export HISTSIZE=5000" >>~/.bash_profile [root@centos7 data]#source ~/.bash_profile [root@centos7 data]#echo $HISTSIZE 5000
【例25】显示历史条件保存的完整路径
[root@centos7 data]#echo $HISTFILE /root/.bash_history
【例27】设置历史命令显示执行的日期和时间
[root@centos7 data]#export HISTTIMEFORMAT="%F %T" [root@centos7 data]#history
十二、Bash中经常使用快捷键:Ctrl+l:清屏,至关于clear命令。Ctrl+c:终止命令。Ctrl+z:挂起命令。Ctrl+a:光标移到命令行首,至关于Home键。Ctrl+e:光标移到命令行尾,至关于End键。Ctrl+u:从光标处删除至命令行尾。Ctrl+k:从光标处删除至命令行尾。Alt+r:删除当前命令行整行。注意在xshell软件中Alt键冲突,若想要使用则须要重置。