简体   繁体   中英

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?

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##*/}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM