tags:

views:

151

answers:

4

What I have is this:

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

Can this be nested (or not) into one line, i.e. a single expression? Basically 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.

A: 

This nesting does not appear to be possible in bash, but it works in zsh:

progname=${${0%.*}##*/}
Nathan Kitchen
+8  A: 

If by nest, you mean something like this:

#!/bin/bash

export HELLO="HELLO"
export HELLOWORLD="Hello, world!"

echo ${${HELLO}WORLD}

Then no, you can't nest ${var} expressions. The bash syntax expander won't understand it.

However, if I understand your problem right, you might look at using the basename command - it strips the path from a given filename, and if given the extension, will strip that also. For example, running basename /some/path/to/script.sh .sh will return script.

Tim
+1 for the basename reference.
Andrew Coleson
A: 

No you can't.

TheBonsai
+1  A: 

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. :)

dannysauer
sorry about the formatting. Just try it. :) var=$( basename ${var%%.*} )
dannysauer
or use the bash-specific basename extension removal as Tim said and I missed... :D
dannysauer