在shell脚本中打印命令名称

时间:2015-03-20 05:30:47

标签: linux shell command

#!/bin/bash

cmdname=${0##*/} 
tmplogfile="/tmp/."$cmdname".log"

echo $cmdname
exit 0

我有上面的shell脚本来做一些任务。 我不明白${0##*/}在这里意味着什么。我将${0##*/}更改为${0},结果相同。有人可以告诉我额外的##*/ 装置

感谢。

1 个答案:

答案 0 :(得分:2)

这是一个shell变量替换符号,用于删除与 ## 之后的表达式匹配的最大前缀模式。此表达式是shell模式而不是正则表达式。在这种情况下,它从$ 0中删除以$结尾的最长前缀。例如,对于以下脚本:

/home/user/> cat /home/user/script
echo 'Value of $0      ' : $0
echo 'Value of ${0##*/}' : ${0##*/}

/home/user/> sh script
Value of $0       : script
Value of ${0##*/} : script

/home/user/> sh /home/user/script
Value of $0       : /home/user/script
Value of ${0##*/} : script

来自FreeBSD sh manpage

  

<强> $ {参数##字}

Remove Largest Prefix Pattern.  The word is expanded to produce a
pattern.  The parameter expansion then results in parameter, with
the largest portion of the    prefix matched by the pattern deleted.

虽然这是FreeBSD sh联机帮助页,但同样适用于所有类似Bourne的shell。

相关问题