Shell Expansion Question

hey all,

x="a1 b1 c2 d2"

echo ${x#*1} b1 c2 d2 
echo ${x##*1} c2 d2 
echo ${x%1*} a1 b

echo ${x%%1*}
a

what does one # stand for?
what does two ## stand for?
what does % mean?
what does two signs %% mean?
what 1* means here?
what *1 means here?

I searched google but I got hard explanation and hard english.
I need someone to explain each command to me in easy words!

thanks.

Did you try the man page for ksh? (man ksh) The man page has the info that you are looking for. If you have additional questions, after reading the specific areas in the man page, one of us can help.

The specific section you're interested in is "parameter expansion". You should find it documented in the man page of any shell that supports those idioms.

Regards,
Alister

${X} is a variable in its full syntax

Mask a regular expression, taking the less greedy portion, starting from left to right.

e.i.
Match as less as possible, zero or more characters, followed by a literal '1'
In your case if x="a1 b1 c2 d2" it matches 'a','1' and stops, returning the remainder as the result. "b1 c2 d2"

Mask a regular expression taking the most greedy portion starting from left to right
e.i
${x##*1} -> match as much as it can. Returns remainder.
if x="a1 b1 c2 d2" then it eats 'a','1',' ','b','1' and stops.

Start from right to left matching as less as possible.

Start from right to left matching as much as possible

Match a literal '1' followed by zero or more characters. (translates to nothing or everything else after a '1' included)

Match zero or more characters followed by a literal '1'. (Translates to everything before a literal '1' or nothing plus '1')

Click me. Some examples half way of the article.

1 Like

Sorry, I'm not the one :rolleyes: however you can take a look at http://wiki.bash-hackers.org/syntax/pe

3 Likes

thank you very much Aia:)
sorry for the late...
thanks danmero

---------- Post updated at 12:02 PM ---------- Previous update was at 10:37 AM ----------

Do Expansion work with csh? because I tried they work only with bash

@eawedat: I don't know about csh. Most of it should also work in ksh.

1 Like