一.shell
shell是操作系统与用户之间的沟通的渠道,可以接收并执行用户的命令,有很多shell程序,目前linux默认使用bash shell程序。
bash shell有很多实用功能,例如:
- 命令记忆
- 命令补全
[TAB][TAB]:列出所有命令
*[TAB][TAB]:列出所有以字符*开头的命令
*[TAB]:列出当前目录以字符*开头的命令
- 后台运行,&,可以不受CTRL+C影响,且单一登录环境中达到多任务目的
- 程序脚本,功能强大
二.变量
跟C语言里的变量意义差不多。
三.变量的显示与设置echo,=,unset
1. 显示变量: echo $变量
liuwanpeng@ubuntu:/etc$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin:/work/setup/gcc-linaro-arm-linux-gnueabihf-4.8-.12_linux/bin
liuwanpeng@ubuntu:/etc$ echo ${PATH}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin:/work/setup/gcc-linaro-arm-linux-gnueabihf-4.8-.12_linux/bin
2. 设置变量, =, 定义与赋值合一了
liuwanpeng@ubuntu:~$ myvar=test_variable
liuwanpeng@ubuntu:~$ echo $myvar
test_variable
liuwanpeng@ubuntu:~$ myvar="test_variable"
liuwanpeng@ubuntu:~$ echo $myvar
test_variable (1). 空格,两边不能直接接空格符,右侧需要用双引号或者单引号扩起来
liuwanpeng@ubuntu:~$ myvar=test variable // 错误,不能有空格,bash会认为空格后面的variable是个命令
variable: command not found
liuwanpeng@ubuntu:~$ myvar="test variable"
liuwanpeng@ubuntu:~$ echo $myvar
test variable
liuwanpeng@ubuntu:~$ var ="lang is $LANG" // 等号左边也不能有空格
No command 'var' found, but there are 22 similar ones
var: command not found (2) 单引号和双引号
liuwanpeng@ubuntu:~$ var="lang is $LANG" // 双引号内的特殊字符,如$还其特殊字符的作用
liuwanpeng@ubuntu:~$ echo $var
lang is en_US.UTF-8
liuwanpeng@ubuntu:~$ var='lang is $LANG' // 单引号内的特殊字符变为普通字符,没有特殊作用了
liuwanpeng@ubuntu:~$ echo $var
lang is $LANG (3) 转意字符可把特殊字符变为普通字符
liuwanpeng@ubuntu:~$ var="lang is \$LANG"
liuwanpeng@ubuntu:~$ echo $var
lang is $LANG (4) 获取其他命令提供的信息, $(命令) 或 `命令`,注意是反引号,数字键1旁边的那个
liuwanpeng@ubuntu:~$ var=$(uname -r)
liuwanpeng@ubuntu:~$ echo $var
4.2.0-27-generic
liuwanpeng@ubuntu:~$ var=`uname -r`
liuwanpeng@ubuntu:~$ echo $var
4.2.0-27-generic (5) 变量的累加
liuwanpeng@ubuntu:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin:/work/setup/gcc-linaro-arm-linux-gnueabihf-4.8-2013.12_linux/bin
liuwanpeng@ubuntu:~$ PATH="$PATH":/home
liuwanpeng@ubuntu:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin:/work/setup/gcc-linaro-arm-linux-gnueabihf-4.8-2013.12_linux/bin:/home
注意:
变量名只能是英文+数字,且只能英文开头;
若变量需要在其他子进程使用,需要使用export使其具有环境变量的作用。
通常大写字符是系统默认变量,小写字符是用户自定义变量
3. 撤销变量unset
liuwanpeng@ubuntu:~$ echo $var
4.2.--generic
liuwanpeng@ubuntu:~$ unset var
liuwanpeng@ubuntu:~$ echo $var
四.环境变量
env:查看环境变量
set:查看所有变量(环境变量+用户变量)
liuwanpeng@ubuntu:~$ env
XDG_VTNR=
XDG_SESSION_ID=c2
CLUTTER_IM_MODULE=xim
SELINUX_INIT=YES
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/liuwanpeng
SESSION=ubuntu
GPG_AGENT_INFO=/run/user//keyring-UUS4MO/gpg::
TERM=xterm
SHELL=/bin/bash
XDG_MENU_PREFIX=gnome-
VTE_VERSION=
WINDOWID=
OLDPWD=/home/liuwanpeng
UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session//
GNOME_KEYRING_CONTROL=/run/user//keyring-UUS4MO
GTK_MODULES=overlay-scrollbar:unity-gtk-module
USER=liuwanpeng
LS_COLORS=rs=:di=;:ln=;:mh=:pi=;:so=;:do=;:bd=;;:cd=;;:or=;;:su=;:sg=;:ca=;:tw=;:ow=;:st=;:ex=;:*.tar=;:*.tgz=;:*.arj=;:*.taz=;:*.lzh=;:*.lzma=;:*.tlz=;:*.txz=;:*.zip=;:*.z=;:*.Z=;:*.dz=;:*.gz=;:*.lz=;:*.xz=;:*.bz2=;:*.bz=;:*.tbz=;:*.tbz2=;:*.tz=;:*.deb=;:*.rpm=;:*.jar=;:*.war=;:*.ear=;:*.sar=;:*.rar=;:*.ace=;:*.zoo=;:*.cpio=;:*.7z=;:*.rz=;:*.jpg=;:*.jpeg=;:*.gif=;:*.bmp=;:*.pbm=;:*.pgm=;:*.ppm=;:*.tga=;:*.xbm=;:*.xpm=;:*.tif=;:*.tiff=;:*.png=;:*.svg=;:*.svgz=;:*.mng=;:*.pcx=;:*.mov=;:*.mpg=;:*.mpeg=;:*.m2v=;:*.mkv=;:*.webm=;:*.ogm=;:*.mp4=;:*.m4v=;:*.mp4v=;:*.vob=;:*.qt=;:*.nuv=;:*.wmv=;:*.asf=;:*.rm=;:*.rmvb=;:*.flc=;:*.avi=;:*.fli=;:*.flv=;:*.gl=;:*.dl=;:*.xcf=;:*.xwd=;:*.yuv=;:*.cgm=;:*.emf=;:*.axv=;:*.anx=;:*.ogv=;:*.ogx=;:*.aac=;:*.au=;:*.flac=;:*.mid=;:*.midi=;:*.mka=;:*.mp3=;:*.mpc=;:*.ogg=;:*.ra=;:*.wav=;:*.axa=;:*.oga=;:*.spx=;:*.xspf=;:
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
SSH_AUTH_SOCK=/run/user//keyring-UUS4MO/ssh
SESSION_MANAGER=local/ubuntu:@/tmp/.ICE-unix/,unix/ubuntu:/tmp/.ICE-unix/
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin:/work/setup/gcc-linaro-arm-linux-gnueabihf-4.8-.12_linux/bin:/home
DESKTOP_SESSION=ubuntu
QT_IM_MODULE=ibus
QT_QPA_PLATFORMTHEME=appmenu-qt5
JOB=dbus
PWD=/home/liuwanpeng
XMODIFIERS=@im=ibus
GNOME_KEYRING_PID==en_US.UTF-
GDM_LANG=en_US
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
IM_CONFIG_PHASE=
COMPIZ_CONFIG_PROFILE=ubuntu
GDMSESSION=ubuntu
SESSIONTYPE=gnome-session
XDG_SEAT=seat0
HOME=/home/liuwanpeng
SHLVL=
LANGUAGE=en_US
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LOGNAME=liuwanpeng
COMPIZ_BIN_PATH=/usr/bin/
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/
QT4_IM_MODULE=xim
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-mtBR2W2AvW
LESSOPEN=| /usr/bin/lesspipe %s
INSTANCE=
TEXTDOMAIN=im-config
XDG_RUNTIME_DIR=/run/user/
DISPLAY=:
XDG_CURRENT_DESKTOP=Unity
GTK_IM_MODULE=ibus
LESSCLOSE=/usr/bin/lesspipe %s %s
TEXTDOMAINDIR=/usr/share/locale/
COLORTERM=gnome-terminal
XAUTHORITY=/home/liuwanpeng/.Xauthority
_=/usr/bin/env
1.重要的变量
(1). PS1设置命令提示符的格式
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
可以修改 /etc/profile,改变PS1的默认值
(2). 本shell的PID:$
liuwanpeng@ubuntu:~$ echo $$
(3).上一个执行命令的回传码?,即执行结果,一般0是正确,其他至是错误
liuwanpeng@ubuntu:~$ ls /no_dir
ls: cannot access /no_dir: No such file or directory
liuwanpeng@ubuntu:~$ echo $? liuwanpeng@ubuntu:~$ ls
liuwanpeng@ubuntu:~$ echo $?
2.环境变量与自定义变量/变量的有效范围
(1).子进程只继承父进程的环境变量,不能继承自定义变量,如果想让父进程的自定义变量可以在子进程里使用,需要用export导出该变量
(2).环境变量~全局变量;自定义变量~局部变量
五. 变量键盘输入 read
-p:后接提示信息
-t: 超时时间
liuwanpeng@ubuntu:~$ read var
input val from keyboard
liuwanpeng@ubuntu:~$ echo $var
input val from keyboard liuwanpeng@ubuntu:~$ read -p "input value:" -t var
input value:my input
liuwanpeng@ubuntu:~$ echo $var
my input
六. 声明变量的类型 declare/typedset
如果不带参数,会显示所有命令,跟set差不多。
declare [-aixr] variable
参数:
-a:将variable的变量定义为数组,array
-i:整型
-x:与export一样
-r:readonly,只读 1. -i,shell默认是字符串,1+2+3不是6,而是字符串“1+2+3”
liuwanpeng@ubuntu:~$ sum=1+2+3
liuwanpeng@ubuntu:~$ echo $sum
1+2+3
liuwanpeng@ubuntu:~$ declare -i sum=1+2+3
liuwanpeng@ubuntu:~$ echo $sum
6 2. -x和-r
liuwanpeng@ubuntu:~$ declare -x sum // export
liuwanpeng@ubuntu:~$ export | grep sum // export命令列出所有带-x的变量,sum带有x属性
declare -ix sum="6"
liuwanpeng@ubuntu:~$ declare +x sum // +号的意思是“取消”
liuwanpeng@ubuntu:~$ export | grep sum // 没有了
liuwanpeng@ubuntu:~$ declare -p sum // -p单独列出变脸的类型
declare -i sum="6"
liuwanpeng@ubuntu:~$ declare -r sum
liuwanpeng@ubuntu:~$ sum=10
bash: sum: readonly variable
七. 数组类型array,shell支持一维数组
liuwanpeng@ubuntu:~$ array[]=
liuwanpeng@ubuntu:~$ array[]=
liuwanpeng@ubuntu:~$ echo ${array[]} // 只有此种形式显示才正常!!! ${}
八.变量内容的删除,替代和替换
1. 删除部分字符
${变量#关键字} | 从左往右,按符合关键字的最短字符处理 |
${变量##关键字} | 从左往右,按符合关键字的最长字符处理 |
${变量%关键字} | 从右往左,按符合关键字的最短字符处理 |
${变量%%关键字} | 从右往左,按符合关键字的最长字符处理 |
liuwanpeng@ubuntu:~$ echo ${PATH}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
liuwanpeng@ubuntu:~$ echo ${PATH#/*sbin:}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games liuwanpeng@ubuntu:~$ echo ${PATH##/*sbin:}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/game liuwanpeng@ubuntu:~$ echo ${PATH%sbin}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games liuwanpeng@ubuntu:~$ echo ${PATH%%sbin}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
2. 替代部分字符
${变量/旧字符/新字符} | 第一个被替换 |
${变量//旧字符/新字符} | 所有都被替换 |
liuwanpeng@ubuntu:~$ echo ${PATH/sbin/SBIN}
/usr/local/SBIN:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin
liuwanpeng@ubuntu:~$ echo ${PATH//sbin/SBIN}
/usr/local/SBIN:/usr/local/bin:/usr/SBIN:/usr/bin:/SBIN:/bin:/usr/games:/usr/local/games:/work/setup/lin/bin
2. 整体替换: new_var=${old_var-content},new_var=${old_var:-content}
liuwanpeng@ubuntu:~$ var_old=old
liuwanpeng@ubuntu:~$ var_new=${var_old-new} // var_old已经设置,后面的new没有赋上,被赋值为var_old
liuwanpeng@ubuntu:~$ echo $var_new
old
liuwanpeng@ubuntu:~$ unset var_old
liuwanpeng@ubuntu:~$ var_new=${var_old-new} // var_old没有设置,var_new被赋值new
liuwanpeng@ubuntu:~$ echo $var_new
new liuwanpeng@ubuntu:~$ var_old="" // 虽为空字符,但是已设置
liuwanpeng@ubuntu:~$ var_new=${var_old-new} // var_old已设置,var_new=$var_old=空字符
liuwanpeng@ubuntu:~$ echo $var_new liuwanpeng@ubuntu:~$ var_new=${var_old:-new} // 冒号把“空字符“视为“未设置“
bash: ${var_old:-new}: bad substitution
liuwanpeng@ubuntu:~$ var_new=${var_old:-new}
liuwanpeng@ubuntu:~$ echo $var_new
new