${0##*/}  是什么意思呢?

我们做一个实验就知道了

新建一个目录——mkdir -p /usr/school/grade/class/

新建一个文件——touch /usr/school/grade/class/student

在student文件里写上代码并保存——

echo $0
echo ${0#*/}
echo ${0##*/}

运行代码——  sh  /usr/school/grade/class/student

我们会得到三个结果分别是

/usr/school/grade/class/student
usr/school/grade/class/student
student

这个时候再来讲解那三条指令

$0,$1,$2 反正前面有一个$美元符号的,都象征着变量,而$0象征本身shell脚本文件的名字,也就是 /usr/school/grade/class/student

最右边的 / ,象征着你要寻找,你要匹配的东西,在哪里找呢?在文件名字 /usr/school/grade/class/student 里面找

#象征要寻找最左边的/

##象征要寻找最右边的/

而*是通配符,象征着任意长度的字符串

所以在##条件下的 */ , 就象征着/usr/school/grade/class/这一段

找到了这一段之后,就把它截取,扔掉,剩下的就是student这个文件名字

所以 ${0##*/} 的作用是寻找一个路径下的具体文件名字

/usr/school/grade/class/student,就得到 student

再看官方解释

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see section 3.5.8 Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the `#' case) or the longest matching pattern (the `##' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

再举一个例子

在suse系统上,这个脚本 /sbin/service

拥有ROOT权限,才能够使用service服务,否则会报错

lijunda@linux-d5wb:~> /sbin/service
service: only root can use service

service这个东西,就是从上面/sbin/service 经过 echo ${0##*/}得到的

可以cat里面的代码验证一下

if test "$(id -u)" -ne 0; then
   echo "${0##*/}: only root can use ${0##*/}" 1>&2
   exit 1
fi

我们再看下一个,${0%/*}

先贴出官网解释

${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the `%' case) or the longest matching pattern (the `%%' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

${0##*/}
${0%/*}

这两个命令的共同点,都是截取,丢掉,前者扔掉的是  */这部分,后者扔掉的是/*这部分

不同点的地方在于,#是从左边头部开始寻找起,%是从尾部开始寻找起(If the pattern matches a trailing portion)

${0%/*} 这个命令,对于/usr/school/grade/class/student,从右边找起,得到是 /student 这个部分,然后扔掉

如果是¥{0%%/*},找到的就是 /usr/school/grade/class/student

综上所述

对于同一个文件 /usr/school/grade/class/student

${0%/*}得到前面/usr/school/grade/class

${0##*/}得到的是后面 student

关于${0##*/} 和${0%/*}相关推荐

  1. libgstreamer-1.0.so.0: cannot open shared object file: No such file or directory

    1. 问题现象 error while loading shared libraries: libgstreamer-1.0.so.0: cannot open shared object file: ...

  2. c+语言+null,C/C++语言中NULL、'\0’和0的区别

    NULL.'\0'和0的值是一样的,都是0,不过它们的表现形式不一样: 1. NULL: 即空指针,不过在C和C++中并不一样.在VS 2013的库文件string.h中可以看到如果定义. 1 /* ...

  3. Ubuntu14.04 64位机上安装cuda8.0+cudnn5.0操作步骤

    查看Ubuntu14.04 64位上显卡信息,执行: lspci | grep -i vga lspci -v -s 01:00.0 nvidia-smi 第一条此命令可以显示一些显卡的相关信息:如果 ...

  4. Spring Cloud Alibaba 基础教程:Nacos 生产级版本 0.8.0

    Spring Cloud Alibaba 基础教程:Nacos 生产级版本 0.8.0 昨晚Nacos社区发布了第一个生产级版本:0.8.0.由于该版本除了Bug修复之外,还提供了几个生产管理非常重要 ...

  5. Silverlight 3发布新版3.0.50106.0

    微软1月19日发布Silverlight 3新版本3.0.50106.0. 该版本主要修复以下几个问题: 问题一: 当使用图形硬件加速功能(GPU)的时候,如果GPU驱动报错,Silverlight ...

  6. AS1.0(2.0)中的XML示例

    虽然Flash早就升级为AS3.0,但是FMS的服务端编程依然仅支持AS1.0(2.0),服务端与.net通讯的最简单方式莫过于请求一个RESTful的webService或wcf,通过它们返回的xm ...

  7. 多数编程语言里的0.1+0.2≠0.3?

    作者 | Parul Malhotra 译者 | Raku 出品 | AI科技大本营(ID:rgznai100) 我们从小就被教导说0.1+0.2=0.3,但是在奇妙的计算机编程世界里面,事情变得不一 ...

  8. flannel原理初探针对0.1.0版本

    flannel flannel是针对k8s设计的三层的网络解决方案.在k8s中为了使pod之间能够使用一种偏平的网络架构,从而完成跨Pod的网络通信. 官网给的原理图如下: flannel 使用TUN ...

  9. GTX 1080Ti + cuda8.0 + cuDNN6.0 安装及测试

    GPU 显卡厂商已经安装好了,直接安装 cuda8.0 + cuDNN6.0 我这里的显卡是 GTX 1080 Ti cuda安装 我下载的是cuda8.0的是deb格式的1.9个G地址:https: ...

  10. PouchContainer 发布 0.3.0 版本,支持 Kubernetes 拥抱 CNCF 生态

    划重点 PouchContainer 是一款轻量级.开源的富容器技术,拥有快速高效.隔离性强.可移植性高.资源占用少等特性,可以帮助企业快速实现存量业务容器化,同时提高超大规模下数据中心的物理资源利用 ...

最新文章

  1. Xcode couldn‘t find any iOS App Development provisioning profiles matching ‘com.example.***‘
  2. [学习笔记]Pollard-Rho
  3. RS232, RS422, RS485 引脚布局区别
  4. 程序员每天晚上都去翻垃圾,竟然年入60万美元?
  5. Qt QApplication 类简介--Qt 类简介专题(四)
  6. 小D课堂 - 新版本微服务springcloud+Docker教程_5-06 高级篇幅之深入源码
  7. 技术人员要分出2成精力防范内斗及斗争方法
  8. mysql bit 设置默认值_为什么数据库字段需要设置默认值
  9. Verilog 教程
  10. 2020西工大计算机考研经验贴
  11. 月模拟题3 201609-3 炉石传说
  12. UOJ #11.【UTR #1】ydc的大树 题解
  13. 浅谈Android相机演变
  14. Codeforces Round #835 (Div. 4) - D. Challenging Valleys
  15. yy欢聚时代软件测试笔试题
  16. 用Python将excel数据插入到MySQL报错(1265, Data truncated for column ‘num‘ at row 1)
  17. 纯手工打war包方法
  18. 理解linux cpu load
  19. 算法系列(十)堆实现优先队列
  20. 环境影响评价图件制作方法

热门文章

  1. 「Python爬虫系列讲解」三、正则表达式爬虫之牛刀小试
  2. C# ComboBox 使用 DataSource SelectedIndex无法设置的问题
  3. Protocol Buffe高级应用话题
  4. 山东理工大学团体设计大赛
  5. RDD的执行流程(简略)
  6. oracle不能写入注册表,保存注册表时提示无法创建值,写入注册表时出错怎么办?...
  7. feed流和瀑布流_基础知识讲解:什么是feed流?
  8. fineui mysql_FineUI配置文件
  9. android webview 清除历史,如何在android中清除webview历史记录
  10. 判断一个数字是否为偶数的几种方式