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.
I know this is an ancient thread, but here are my 2 cents.
Here's an (admittedly kludgy) bash function which allows for the required functionality:
Here's a short test script:
The output is, as expected:
This nesting does not appear to be possible in bash, but it works in zsh:
eval will allow you to do what you are wanting:
Output:
Hello, world
Expressions like
${${a}}
do not work. To work around it, you can useeval
:Output is
value
If by nest, you mean something like this:
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, runningbasename /some/path/to/script.sh .sh
will returnscript
.Actually it is possible to create nested variables in bash, using two steps.
Here is a test script based upon the post by Tim, using the idea suggested by user1956358.
The output is:
There are lots of neat tricks explained by running 'info bash' from the command line, then searching for 'Shell Parameter Expansion'. I've been reading a few myself today, just lost about 20 minutes of my day, but my scripts are going to get a lot better...
Update: After more reading I suggest this alternative per your initial question.
It returns