Bash Shell 内置变量命令

Bash Shell 内置变量命令

  • 常用的内置命令:echo、eval、exec、export 、read 、shift

echo在屏幕上输出信息

  • 常用参数
参数选项 说明
-n 不换行输出
-e 解析转义字符
转义字符
\n 换行
\r 回车
\t 制表符
\b 退格
\v 纵向制表符
  • 案例
# echo shell;echo bash
shell
bash
# echo -n shell;echo bash
shellbash
[root@lhf test]# echo "shell\tbash\nshell\tbash"
shell\tbash\nshell\tbash
[root@lhf test]# echo -e "shell\tbash\nshell\tbash"
shell   bash
shell   bash
# printf "shell\tbash\nshell\tbash\n"
shell   bash
shell   bash
[root@lhf test]# echo -e "1\b23"
23
[root@lhf test]# printf  "1\b23\n"
23
[root@lhf test]# echo -e "1\b23"
23
[root@lhf test]# printf  "1\b23\n"
23

eval

  • 功能:当执行eval语句时,shell读入参数args,将它们组合成一个新命令

  • 案例

[root@lhf test]# cat novel.sh 
echo \$$#
[root@lhf test]# bash novel.sh 1 2
$2
[root@lhf test]# cat eval.sh
eval "echo \$$#"
[root@lhf test]# bash eval.sh 1 2
2


exec

  • exec命令可以在不创建新的子进程的前提下,转去执行指定的命令,当指定命令执行完毕之后,该进程终止
[root@lhf test]# su - lhf
[lhf@lhf ~]$ exec date
Sat Nov 16 23:05:38 CST 2019
[root@lhf test]#
  • 当exec打开文件时,read命令每次都会将文件指针移动文件的下一行进行读取,直到文件结尾。
[root@lhf test]# seq 5 > /tmp/tmp.log
[root@lhf test]# cat exec.sh 
exec < /tmp/tmp.log
while read line
do
    echo $line
done
echo ok
[root@lhf test]# bash exec.sh 
1
2
3
4
5
ok

read

  • 功能:从标准输入读取字符串等信息,传给shell程序的内部定义变量。

shift

  • 功能:按照定义方式重新命名所有位置参数变量,比如2变成1,3变成2,
[root@lhf test]# bash shift.sh 1 2
1 2
2
[root@lhf test]# cat shift.sh 
echo  $1 $2
if [ $# -eq 2 ];then
    shift
    echo $1
fi
[root@lhf test]# bash shift.sh 1 2
1 2
2


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,500评论 0 5
  • exec 功能:exec能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该进程(也就是...
    lukeyan阅读 870评论 0 0
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,861评论 0 10
  • 概述 首先,咱们来了解一下,什么是Shell。操作系统内核给我们提供了各种接口,同时也提供了各种用户层的库,理论上...
    keysaim阅读 1,434评论 0 0
  • 最重要的事情 客栈的清洁整理 进步 1.客栈线路改造时,要以最少可交付产品的原则来做。首先的不要做复杂的设计,先做...
    老陈看世界阅读 201评论 0 0