3

Could anyone eloborate how ##*/ works in UNIX Shell scripting. I have seen it's use in Korn Shell. It is specifically used for removing extension of file.

e.g. func_write_app_log "o Deleting status file ${CIE_STATUS_FILE##*/}"

Here suppose the file is CIE_STATUS_FILE.DAT, then ##*/ will display CIE_STATUS_FILE

ps26262
  • 31
  • 2
  • You can remove prefixes and suffixes matching patterns with these constructs. These are specified by POSIX and work in many shells, e.g. zsh, bash, ksh and even plain old bourne shell (sh). The reference is http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 – Jens Aug 29 '11 at 13:30

1 Answers1

5

This also works in Bash and is described here:

${string##substring}

Deletes longest match of $substring from front of $string.

The * is a wildcard which means match anything. Your example removes the path from the file, not the extension.

$ bazfile='/foo/bar/baz.txt'
$ echo ${bazfile##*/}
baz.txt

To remove the extension you want to use %:

${string%substring}

Deletes shortest match of $substring from back of $string.

$ echo ${bazfile%.*}
/foo/bar/baz
Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452