FatalKeystroke
FatalKeystroke

Reputation: 3062

bash: How to properly get basename into a string?

Here's my snippet:

__VERSION="0.0.1"
__COMMAND="${basename}"
__USAGE="$__COMMAND -abc args"
if [ $# == 0 ] ; then
    echo "Usage: ${__USAGE}"
    exit 1;
fi

Every time I run it I get:

Usage: -abc args

When I'm expecting the output to be:

Usage: filename -abc args

I've tried:

__COMMAND=basename

__COMMAND=basename $0

__COMMAND="$basename"

__COMMAND="${basename}"

__COMMAND="${basename $0}"

Though the error has been different in some cases, none of them have worked.

What would be the proper way of doing this?

Upvotes: 3

Views: 516

Answers (1)

Carl Norum
Carl Norum

Reputation: 225082

The correct one is about the only one you didn't try:

__COMMAND=$(basename $0)

But you don't need basename at all. You can just use bash parameter expansion:

__COMMAND=${0##*/}

Upvotes: 8

Related Questions