LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   string substitution (http://www.linuxquestions.org/questions/linux-newbie-8/string-substitution-790705/)

lipun4u 02-22-2010 02:50 AM

string substitution
 
I was reading string substitution...


str=abcABC123ABCabc

echo ${str#a*C}
echo ${str##a*C}


Can somebody explain me how it works ??

micxz 02-22-2010 03:03 AM

Code:

str=abcABC123ABCabc
#  |----|          shortest
#  |----------|    longest

echo ${str#a*C} # 123ABCabc
# Strip out shortest match between 'a' and 'C'.

echo ${str##a*C} # abc
# Strip out longest match between 'a' and 'C'.

quote from abs guide'

neonsignal 02-22-2010 03:05 AM

Both of these are string deletions. The expression that follows the # is the pattern that is being matched (and deleted), in this case 'a*C'. In other words, the pattern is 'a', followed by 0 or more characters, followed by a 'C'.

The difference between the two examples is that the first does a minimal match (ie, finds the match from the start of the string that is the shortest), while the second does a maximal match (ie, finds the match from the start of the string that is the longest).

micxz 02-22-2010 03:19 AM

reading this again I think I just quoted what your reading. So thanks to neonsignal its explained. strip out is the key word here. I hope you got it now'


All times are GMT -5. The time now is 05:19 PM.