shell学习之常用bash内置变量

常用的Bash内置变量



1 BASH_COMMAND当前执行的命令


2 LINENO显示当前所在行号

/bin/bash

           echo "this is test about ARG LINENO"

           echo "line now in :$LINENO"

结果为      

[root@www shell]# ./bash.sh

this is test about ARG LINENO

line now in :   4

方便调试

3 FUNCNAME[i]在第几层函数

#!/bin/bash

#

function test1(){

echo "test1:FUNCNAME[0] is ${FUNCNAME[0]}"

echo "test1:FUNCNAME[1] is ${FUNCNAME[1]}"

echo "test1:FUNCNAME[2] is ${FUNCNAME[2]}"

test2

}

function test2(){

echo "test2:FUNCNAME[0] is ${FUNCNAME[0]}"

echo "test2:FUNCNAME[1] is ${FUNCNAME[1]}"

echo "test2:FUNCNAME[2] is ${FUNCNAME[2]}"

}

test1

          运行结果为

liuliancao@liuliancao-K45VD:~/shell$ ./bash1.sh

test1:FUNCNAME[0] is test1

#此时第一层func为本函数,所以是test1

test1:FUNCNAME[1] is main

#第二层为二层函数,应该为main,称为主脚本,调用test1的函数

test1:FUNCNAME[2] is

#再下层就没有了,下面的同理

test2:FUNCNAME[0] is test2

test2:FUNCNAME[1] is test1

test2:FUNCNAME[2] is main

#这里可以方便调试

4 HOSTNAME   HOSTTYPE    HOME  UID  GROUPS

这些大家应该都比较熟悉,我们看一下我的主机中它们的值

liuliancao@liuliancao-K45VD:~/shell$ echo $HOSTNAME;echo $HOSTTYPE;echo $HOME;echo $GROUPS

liuliancao-K45VD

x86_64

/home/liuliancao

1000

liuliancao@liuliancao-K45VD:~/shell$ echo $UID

1000

5 OLDPWD  上个工作目录  PWD 当前工作目录

liuliancao@liuliancao-K45VD:~/shell$ echo $PWD;echo $OLDPWD

/home/liuliancao/shell

/etc

6 $$ SHELL本身的PID

liuliancao@liuliancao-K45VD:~/shell$ echo $$

22535

liuliancao@liuliancao-K45VD:~/shell$ ps -e | grep bash

22535 pts/6    00:00:00 bash

7 RANDOM 随机数生成器

#random.sh

#!/bin/bash

echo "this is a random nu from 0 to 30"

scope=30

num1="`expr $RANDOM % $scope + 0`"

num2="`expr $RANDOM % $scope + 0`"

echo "nu1: ${num1}"

echo "nu2: ${num2}"

运行结果为:

liuliancao@liuliancao-K45VD:~/shell$ ./random.sh

this is a random nu from 0 to 30

nu1: 20

nu2: 8

liuliancao@liuliancao-K45VD:~/shell$ ./random.sh

this is a random nu from 0 to 30

nu1: 24

nu2: 15

#相当实用的

8 REPLY read没有变量名时此时,内容保存在该变量中

#reply.sh

#!/bin/bash

read -p "no arg test:"

echo "\$REPLY is  $REPLY"

结果为:

liuliancao@liuliancao-K45VD:~/shell$ ./reply.sh

no arg test:hello ok

$REPLY is  hello ok

#说不定什么时候用到这个变量

9 SECONDS脚本运行的时间

#seconds.sh

#!/bin/bash

sleep 2

echo "script run $SECONDS already!"

结果为

liuliancao@liuliancao-K45VD:~/shell$ ./seconds.sh

script run 2 already!

#可以很方便地测试脚本运行性能呢

10  GLOBIGNORE对通配符忽略某些通配模式

/shell文件夹下有如下的文件

liuliancao@liuliancao-K45VD:~/shell$ ls -al

total 24

drwxrwxr-x  2 liuliancao liuliancao 4096  520 19:40 .

drwxr-xr-x 45 liuliancao liuliancao 4096  520 19:37 ..

-rw-rw-r--  1 liuliancao liuliancao    0  520 19:40 1.txt

-rw-rw-r--  1 liuliancao liuliancao    0  520 19:40 2.html

-rw-rw-r--  1 liuliancao liuliancao    0  520 19:40 3.c

-rwxrwxr-x  1 liuliancao liuliancao  324  520 19:02 bash1.sh

-rwxrwxr-x  1 liuliancao liuliancao  182  520 19:28 random.sh

-rwxrwxr-x  1 liuliancao liuliancao   71  520 19:36 reply.sh

-rwxrwxr-x  1 liuliancao liuliancao   68  520 19:37 seconds.sh

-rw-rw-r--  1 liuliancao liuliancao    0  520 19:40 .xmind

如果我们修改下

liuliancao@liuliancao-K45VD:~/shell$ GLOBIGNORE=*.txt

liuliancao@liuliancao-K45VD:~/shell$ ls *

2.html  3.c  bash1.sh  random.sh  reply.sh seconds.sh  .xmind

liuliancao@liuliancao-K45VD:~/shell$ rm *.txt

rm: cannot remove ‘*.txt’: No such file or directory

#hah,成功隐藏了文件,不过只是对通配符有关,其实我们可以用来对制定文件夹内的一些满足某种模式的进行忽略,其他的进行自己想要的操作,上面的就可以删除除了.txt的文件,修改为rm *

11 IFS (Internal Field Separator)  空格分隔的内容

#ifs.sh

#!/bin/bash

#save orign IFS,and set it to :

OIFS=$IFS

IFS=":"

#next manage to read the val with separator :

read -p "input something with separator :" h1 h2 h3

echo "h1:${h1}"

echo "h2:${h2}"

echo "h3:${h3}"

#return to orign

IFS=$OIFS

运行结果为

liuliancao@liuliancao-K45VD:~/shell$ ./ifs.sh

input something with separator :liuliancao : qixue : linux

h1:liuliancao

h2: qixue

h3: linux

12 PATH 执行文件查找路径

通常命令执行前会去PATH变量按顺序查找是否有该命令,找到就执行

第一次搜索完这个命令,系统就会把命令的路径保存在hash表中,方便以后实用,实用hash -r可以清空hash

关于PATH如果PATH开头有一个:是挺危险的,它会扩展为当前目录,此时目录下如果有一个ls命令,则可获得root的权限


13 TMOUT 用于read select 交互式shell,为其指定超时时间

#tmout.sh

#!/bin/bash

TMOUT=3

echo "you only have 3 seconds to input "

read -p  "hah,just a test:" test

echo "you entered $test"

结果为

liuliancao@liuliancao-K45VD:~/shell$ ./tmout.sh

you only have 3 seconds to input

hah,just a test:you entered


最后补充一下

SHELLOPTS的一些

对应一个命令shopt

-p 表示print显示 -u 表示停止选项 -s 表示启用选项

liuliancao@liuliancao-K45VD:~/shell$ shopt -p

shopt -u autocd

shopt -u cdable_vars #cd参数可以是变量

shopt -u cdspell #纠正拼写错误

shopt -u checkhash #执行命令先在hash表中查找

shopt -u checkjobs #

shopt -s checkwinsize #检查窗口大小,自动更新linescolumn

shopt -s cmdhist #多行命令保存为一行存在history

shopt -u compat31

shopt -u compat32

shopt -u compat40

shopt -u compat41

shopt -u compat42

shopt -s complete_fullquote

shopt -u direxpand

shopt -u dirspell

shopt -u dotglob

shopt -u execfail #交互式shell不会因为exec内置命令不能执行而退出

shopt -s expand_aliases #别名被扩展

shopt -u extdebug

shopt -s extglob #打开扩展通配

shopt -s extquote

shopt -u failglob

shopt -s force_fignore

shopt -u globstar

shopt -u globasciiranges

shopt -u gnu_errfmt

shopt -s histappend #shell退出,历史清单会添加到histfile文件,而不是覆盖

shopt -u histreedit

shopt -u histverify

shopt -u hostcomplete

shopt -u huponexit

shopt -s interactive_comments #表示#开头的语句可以被忽略

shopt -u lastpipe

shopt -u lithist

shopt -u login_shell

shopt -u mailwarn

shopt -u no_empty_cmd_completion

shopt -u nocaseglob

shopt -u nocasematch

shopt -u nullglob

shopt -s progcomp

shopt -s promptvars

shopt -u restricted_shell

shopt -u shift_verbose

shopt -s sourcepath#如果设置,source内置命令使用PATH的值来寻找参数

shopt -u xpg_echo

总结 其实bash内置变量表面上看来挺枯燥,其实还是挺有意思,需要自己去体会哦。




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。