Skip to main content
added 215 characters in body
Source Link
mwfearnley
  • 3.5k
  • 2
  • 37
  • 37

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax with other characters to trim filenames:

  • ${A##*/} removes all containing folders, if any, from the start of the path, e.g.
    /usr/bin/git -> git
    /usr/bin/ -> (empty string)
    /usr/bin -> bin

  • ${A%/*} removes the last file/folder/trailing slash, if any, from the end:
    /usr/bin/git -> /usr/bin
    /usr/bin/ -> /usr/bin
    /usr/bin -> /usr/

  • ${A%.*} removes the last extension, if any (just be wary of things like /my.path/noext):
    archive.tar.gz -> archive.tar
    /my.path/noext -> /my (!)

To avoid the last issue, you use an explicit extension if you know it: %.ext instead of %.*

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax to trim filenames:

  • ${A##*/} removes all containing folders, if any, from the start of the path, e.g.
    /usr/bin/git -> git
    /usr/bin/ -> (empty string)

  • ${A%/*} removes the last file/folder/trailing slash, if any, from the end:
    /usr/bin/git -> /usr/bin
    /usr/bin/ -> /usr/bin

  • ${A%.*} removes the last extension, if any (just be wary of things like my.path/noext):
    archive.tar.gz -> archive.tar

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax with other characters to trim filenames:

  • ${A##*/} removes all containing folders, if any, from the start of the path, e.g.
    /usr/bin/git -> git
    /usr/bin/ -> (empty string)
    /usr/bin -> bin

  • ${A%/*} removes the last file/folder/trailing slash, if any, from the end:
    /usr/bin/git -> /usr/bin
    /usr/bin/ -> /usr/bin
    /usr/bin -> /usr/

  • ${A%.*} removes the last extension, if any (just be wary of things like /my.path/noext):
    archive.tar.gz -> archive.tar
    /my.path/noext -> /my (!)

To avoid the last issue, you use an explicit extension if you know it: %.ext instead of %.*

added 241 characters in body
Source Link
mwfearnley
  • 3.5k
  • 2
  • 37
  • 37

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax to trim filenames:

  • ${A##*/} removes all containing folders (e.g. /usr/bin/git -> git)

    ${A##*/} removes all containing folders, if any, from the start of the path, e.g.
    /usr/bin/git -> git
    /usr/bin/ -> (empty string)

  • ${A%.*} removes the last extension (e.g. archive.tar.gz -> archive.tar) - just be wary of things like my.path/noext

    ${A%/*} removes the last file/folder/trailing slash, if any, from the end:
    /usr/bin/git -> /usr/bin
    /usr/bin/ -> /usr/bin

  • ${A%.*} removes the last extension, if any (just be wary of things like my.path/noext):
    archive.tar.gz -> archive.tar

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax to trim filenames:

  • ${A##*/} removes all containing folders (e.g. /usr/bin/git -> git)
  • ${A%.*} removes the last extension (e.g. archive.tar.gz -> archive.tar) - just be wary of things like my.path/noext

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax to trim filenames:

  • ${A##*/} removes all containing folders, if any, from the start of the path, e.g.
    /usr/bin/git -> git
    /usr/bin/ -> (empty string)

  • ${A%/*} removes the last file/folder/trailing slash, if any, from the end:
    /usr/bin/git -> /usr/bin
    /usr/bin/ -> /usr/bin

  • ${A%.*} removes the last extension, if any (just be wary of things like my.path/noext):
    archive.tar.gz -> archive.tar

Try and make answer more helpful
Source Link
mwfearnley
  • 3.5k
  • 2
  • 37
  • 37

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A%%A% *} - remove longestshortest trailing * (keep onlystrip the firstlast word)
  • ${A%A%% *} - remove shortestlongest trailing * (keepstrip the all but the last wordlast words)
  • ${A##*A#* } - remove longestshortest leading * (keep onlystrip the lastfirst word)
  • ${A#*A##* } - remove shortestlongest leading * (keepstrip the all but the first wordfirst words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax to trim filenames:

  • ${A##*/} removes all containing folders (e.g. /usr/bin/git -> git)
  • ${A%.*} removes the last extension (e.g. archive.tar.gz -> archive.tar) - just be wary of things like my.path/noext

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A%% *} - remove longest trailing * (keep only the first word)
  • ${A% *} - remove shortest trailing * (keep all but the last word)
  • ${A##* } - remove longest leading * (keep only the last word)
  • ${A#* } - remove shortest leading * (keep all but the first word)

Of course a "word" here may contain any character that isn't a literal space.

The documentation is a bit painful to read, so I've summarised it in a simpler way.

Note that the '*' needs to swap places with the ' ' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

  • ${A% *} - remove shortest trailing * (strip the last word)
  • ${A%% *} - remove longest trailing * (strip the last words)
  • ${A#* } - remove shortest leading * (strip the first word)
  • ${A##* } - remove longest leading * (strip the first words)

Of course a "word" here may contain any character that isn't a literal space.

You might commonly use this syntax to trim filenames:

  • ${A##*/} removes all containing folders (e.g. /usr/bin/git -> git)
  • ${A%.*} removes the last extension (e.g. archive.tar.gz -> archive.tar) - just be wary of things like my.path/noext
Swap order
Source Link
mwfearnley
  • 3.5k
  • 2
  • 37
  • 37
Loading
Source Link
mwfearnley
  • 3.5k
  • 2
  • 37
  • 37
Loading