averageUsername123
averageUsername123

Reputation: 723

Basename isn't able to take in file names with dashes

I'm writing a bash script that needs to take in a file name, and it may be something like:

"filename - Copy.txt"

When I try to get the file name of this file, basename seems to pick up the dash as an operand and fails.

basename: extra operand "Copy"

I have some like this:

echo $(basename -- $eachFile)

Upvotes: 5

Views: 5122

Answers (1)

John Weldon
John Weldon

Reputation: 40789

Use echo $(basename "$eachFile") to avoid the bash interpreter from seeing the spaces in the file name as spaces between arguments to basename

Upvotes: 5

Related Questions