Home > other >  A fast and proper way to get the basename of a directory or a file in bash
A fast and proper way to get the basename of a directory or a file in bash

Time:10-17

I have this code:

get_base() { echo "${1##*/}"; }

And this works fine until we have 1 or more trailing slashes

I have found a solution on how to fix it, but the problem is that we'll need extglob enabled, and I don't want that:

ari@ari-gentoo ~ % x='/home/ari///'

ari@ari-gentoo ~ % echo "${x%% (/)}"
/home/ari

Then we can obv save it into a tmp var and run the basename substitution on it

Anyway, my question is, is there any proper way to do it and it still being fast (meaning no calls to external commands because this function gets called quite a lot) without needing any fancy features enabled?

Thanks for the answers in advance :)

Questions and answers

no as all of those solutions either use commands or have a substitution expression that only strips the last slash, not multiples :) (e.g. /home/ari/// with expr ${x%/} would become only /home/ari// when it needs to be /home/ari)

  • What do you mean by 'proper'

By proper I mean 'achieved without enabling extglob or any other fancy features'

CodePudding user response:

Here is one way:

get_base() {
  set -- "${1%"${1##*[!/]}"}"
  printf '%s\n' "${1##*/}"
}
$ get_base /home/oguz//
oguz
$ get_base /root
root
$ get_base /

$ get_base .bash_history
.bash_history

CodePudding user response:

Ideas:

get_base() { [[ $1 =~ ([^/]*)/*$ ]]; echo "${BASH_REMATCH[1]}"; };
get_base() { while [[ "${1%/}" != "$1" ]]; do set -- "${1%%/}"; done; echo "${1##*/}"; }
  • Related