Variable clarification

what is the significance of %%.ksh in processname and %.ksh in processname_1 variable?
Why is it returning same value?How is it working?

processname=Testabc
export processname=${processname%%.ksh}
echo $processname #It is returning Testabc
export processname_1=${processname%.ksh}
echo $processname_1   #It is returning Testabc

Try a slightly different example:

processname=Testabc.1.2.3.ksh
export processname_1=${processname%.*.ksh}
echo $processname_1
export processname=${processname%%.*.ksh}
echo $processname

The variable expansion ${variable%pattern} expands the contents of variable removing the shortest string at the end of the contents that is matched by the filename matching pattern pattern.

The variable expansion ${variable%%pattern} expands the contents of variable removing the longest string at the end of the contents that is matched by the filename matching pattern pattern.

When you run the above commands with a ksh after setting the verbose and trace flags:

set -xv

you will see output similar to:

processname=Testabc.1.2.3.ksh
+ processname=Testabc.1.2.3.ksh
export processname_1=${processname%.*.ksh}
+ processname_1=Testabc.1.2
+ export processname_1
echo $processname_1
+ echo Testabc.1.2
Testabc.1.2
export processname=${processname%%.*.ksh}
+ processname=Testabc
+ export processname
echo $processname
+ echo Testabc
Testabc

When there aren't any special pattern matching characters in pattern (as occurred in your original example where pattern was .ksh , there is no difference between ${processname%%.*.ksh} and ${processname%.*.ksh} . When pattern doesn't match any characters at the end of the value of variable, the expansion behaves as if ${variable} had been specified (which is what happened in your example, since there was no .ksh at the end of the contents of your variable to be removed).

You can also use ${variable#pattern} and ${variable##pattern} to remove the shortest and longest, respectively, leading matching characters from the expansion.

1 Like

A way to remember which end of the variable you are removing from is that for numbers, the % would be written at the end, so the end is removed.

The often misnamed octothorpe # is more usually referred to as a hash or wrongly as a pound (the pound is ) however if think that a currency amount of whole units is normally written as �12.34 or $12.34 , or even �12.34 then the octothorpe (okay, I call it a hash too) trims from the beginning of the string.

I hope that this helps, but please forgive me being patriotic a pound being , although I suppose in weight it's also lb so maybe I should just give up. :o

Robin