2

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!

phototrek
  • 23
  • 2

2 Answers2

3

How avoid basename altogether and just do a

folder=$directory:t

BTW, if you want the equivalent to dirname (i.e. the directory portion), it would be $directory:h.

user1934428
  • 19,864
  • 7
  • 42
  • 87
2

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

folder=`basename -- "$directory"`
lolesque
  • 10,693
  • 5
  • 42
  • 42