Shell脚本中$ {var#?}是什么意思

时间:2020-02-13 04:20:04

标签: shell

我不知道什么#?是的,我走了,什么也没找到

完整文件:tinode/chat

<body id="body" class="lightMode">
	
<label>
  <input type="checkbox" id="darkModeSwitch" title="Toggle Light/Dark Mode">
</label>

<br><br>
<p>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</body>

2 个答案:

答案 0 :(得分:2)

${tag#?}扩展为$tag的值,其中第一个字符被删除。

引用POSIX shell specification

${parameter#[word]}
删除最小前缀模式。 单词应为 展开以产生图案。然后参数扩展 结果为 parameter ,且前缀的最小部分匹配 由图案删除。如果存在, word 不得以 未引用的'#'

在这种情况下,模式为?,它与单个字符匹配。

如果您使用的是bash,则Bash manual也涵盖了这一点(您可以使用info bash在系统上查看手册)

'${PARAMETER#WORD}'
'${PARAMETER##WORD}'
     The WORD is expanded to produce a pattern just as in filename
     expansion (*note 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.

答案 1 :(得分:-1)

#!/bin/bash
tag='123'
version=${tag#?}
echo ${version}  # output is: 23

=======
所以#?是删除第一个字符?

相关问题