shell总结(0基础入门)

 

一、简介

shell是用户和操作系统交互的命令行解释器。

shell有很多种:

  bash、csh、sh、ksh、、、

 

我们等了linux时看到的命令行就是一个bash。 

 

二、第一个脚本:

[root@linux1 script]# vim first.sh
#!/bin/bash
#auther:xiaofan
#time:2016.10.4
#井号是注释
echo "hello word"

 

执行脚本:

方法1:

[root@linux1 script]# bash first.sh
hello word

[root@linux1 script]# sh first.sh
hello word

方法2:
[root@linux1 script]# chmod +x first.sh
[root@linux1 script]# ./first.sh
hello word

 

查看脚本执行的过程:

[root@linux1 script]# sh -x first.sh
+ echo 'hello word'
hello word

 

三、变量

 

变量分为两大类:

  局部变量:需要自己定义。

  环境变量:系统自带的。

 

变量定义:

  变量名称=变量值

 

调用自定义变量:

[root@linux1 script]# name="xiaofan"
[root@linux1 script]# echo "my name is $name"
my name is xiaofan
[root@linux1 script]# echo my name is $name
my name is xiaofan
[root@linux1 script]# echo 'my name is $name'   #注意单引号是不会解释变量的
my name is $name

 

 

常用的系统变量: 

[root@linux1 script]# cat t1.sh
#!/bin/bash
echo $0      #当前程序名
echo $1  #程序的第一个参数
echo $*  #程序的所有参数名
echo $#  #当前程序的参数个数
echo $?       #程序执行的返回状态
echo $PATH    #系统环境变量的路径
echo $PWD      #当前目录
echo $UID        #当前用的id
[root@linux1 script]# sh t1.sh t1 t2
t1.sh
t1
t1 t2
2
0
/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/root/bin
/script
0

 

 

三、脚本输出有颜色的字体:

\033[32m  和 \033[0m  之间的字体会变成绿色

\033[32m  和 \033[1m  下面的所有的字体会变成绿色

\033[31m  和 \033[0m  之间的字体会变成红色

 

 

转载于:https://www.cnblogs.com/fanxuanhui-linux/p/5931503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值