codefx
codefx

Reputation: 10502

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

I am trying to understand a shell script. What does ${device_info##* } do?

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

Upvotes: 1

Views: 1066

Answers (1)

eckes
eckes

Reputation: 10423

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

Upvotes: 4

Related Questions