Can ${var} parameter expansion expressions be nest

2019-01-01 07:17发布

What I have is this:

progname=${0%.*}
progname=${progname##*/}

Can this be nested (or not) into one line, i.e. a single expression?

I'm trying to strip the path and extension off of a script name so that only the base name is left. The above two lines work fine. My 'C' nature is simply driving me to obfuscate these even more.

标签: bash
13条回答
宁负流年不负卿
2楼-- · 2019-01-01 08:07

The basename bultin could help with this, since you're specifically splitting on / in one part:

user@host# var=/path/to/file.extension
user@host# basename ${var%%.*}
file
user@host#

It's not really faster than the two line variant, but it is just one line using built-in functionality. Or, use zsh/ksh which can do the pattern nesting thing. :)

查看更多
登录 后发表回答