2

I have a bash script and I see in the code

${variable#!} 

what it means? thanks a lot

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
karmax
  • 171
  • 2
  • 13

3 Answers3

4

it removes the leading ! from the value of the variable.

an example would be:

kent$  foo='!!hello'   

kent$  echo ${foo#!} 
!hello
Kent
  • 189,393
  • 32
  • 233
  • 301
  • 1
    Technically it doesn't remove it *from the variable*, it expands to the content of the variable with it removed. I know you know that but it might be worth mentioning that it doesn't change the contents of the variable without reassignment. – Adrian Frühwirth Sep 05 '14 at 09:25
  • @AdrianFrühwirth you are right. it won't change `foo` in this example. – Kent Sep 05 '14 at 09:27
2

See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

${parameter#word}

The word is expanded to produce a pattern just as in 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. 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.

So, if $variable begins with !, it will return its content without the first !. If not, it will return $variable.

Juan Cespedes
  • 1,299
  • 12
  • 27
1

demo:

var="!some!some!"
echo ${var#!}

prints

some!some!
clt60
  • 62,119
  • 17
  • 107
  • 194