zshfilenamesbasename

basename of file with a leading - (dash)


I have a problem with basename in a zsh script. Imagine $directory containing a file name with a leading dash, in my case it's "-Fast-". Then the script executes

        folder=$(basename "$directory")

or if I try the other syntax of

        folder=`basename "$directory"`

it both leads to the same error:

basename: illegal option -- F usage: basename string [suffix] basename [-a] [-s suffix] string [...]

Other than not using files with a leading dash, which may be hard to explain to the common user, what do I do? Thanks!


Solution

  • In most commands, you can use a double dash -- to tell "end of arguments".

    folder=`basename -- "$directory"`