Kevin Powell
Kevin Powell

Reputation: 601

What does ${1%..} mean in this shell script?

I got script like this,

latex $1
asy ${1%.tex}.asy

I know if $1=test.tex, then ${1%.tex}.asy will be test-1.asy, but what does 1%. mean here? And if I want ${1%.tex}.asy to be test.asy, what should I do?

Upvotes: 3

Views: 8085

Answers (2)

gniourf_gniourf
gniourf_gniourf

Reputation: 46853

From the Bash Reference Manual:

${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.

So if $1 expands to text.tex then ${1%.tex} expands to text.

Upvotes: 6

John1024
John1024

Reputation: 113924

You can provide arguments to your script, e.g.:

myscript one two three

Those arguments are assigned to positional parameters $1, $2, and $3. In the example above, $1 would be assigned to one and $2 to two, etc. The code ${1%.tex}.asy is just operating on the variable $1: it returns a string with the suffix .tex removed and replaced it with .asy.

Upvotes: 3

Related Questions