codefx codefx - 4 years ago 166
Bash Question

What does ${xyz##* } mean in a shell file?

I am trying to understand a shell script. What does

${device_info##* }

device_info=$(ls -l /dev/disk/by-id/my-disk)
relative_path=${device_info##* }

Answer Source

${x##p} is a parameter expansion in posix or bourne shell which removes the longest (because of ##, there is also a shortest match with # operator) match of p from the shell variable x. The (file name globbing, not regular expression) pattern which is actually matched in your case is '*' (which means any number characters terminated by a blank).

You can test it yourself:

A="a b cc"
echo longest: ${A##* }
echo shortest: ${A#* }

Which will print longest cc and shortest b cc.

In your case it will remove all but the last column from the ls output.

The patterns are documented in the shell man page (Parameter Expansion section in man bash). It is also documented in section 2.6.2 of the Open Group posix shell command language.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download