Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

percent symbol in Bash, what's it used for? [duplicate]

Tags:

bash

I have a filename which ends with .zip and I wanted just the filename without zip. Here I found a trick in bash.

$f="05 - Means-End Analysis Videos.zip" $echo "${f%*.zip}" 05 - Means-End Analysis Videos 

What is happening here? How come %*.zip is removing my extension?

like image 934
Senthil Kumaran Avatar asked Jan 22 '16 16:01

Senthil Kumaran


1 Answers

Delete the shortest match of string in $var from the beginning:

${var#string} 

Delete the longest match of string in $var from the beginning:

${var##string} 

Delete the shortest match of string in $var from the end:

${var%string} 

Delete the longest match of string in $var from the end:

${var%%string} 

Try:

var=foobarbar echo "${var%b*r}" > foobar echo "${var%%b*r}" > foo 
like image 154
pfnuesel Avatar answered Oct 02 '22 15:10

pfnuesel