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.
The following option has worked for me:
Output is:
Bash supports indirect expansion:
This should support the nesting you are looking for.
If the motivation is to "obfuscate" (I would say streamline) array processing in the spirit of Python's "comprehensions", create a helper function that performs the operations in sequence.
You can use the result with a nice one-liner.
There is a 1 line solution to the OP's original question, the basename of a script with the file extension stripped:
Here's another, but, using a cheat for basename:
Other answers have wandered away from the OP's original question and focused on whether it's possible to just expand the result of expressions with
${!var}
but came across the limitation thatvar
must explicitly match an variable name. Having said that, there's nothing stopping you having a 1-liner answer if you chain the expressions together with a semicolon.If you want to make this appear like a single statement, you can nest it in a subshell, i.e.
An interesting usage is indirection works on arguments of a bash function. Then, you can nest your bash function calls to achieve multilevel nested indirection because we are allowed to do nested commands:
Here's a demonstration of indirection of an expression:
Here's a demonstration of multi level indirection thru nested commands:
An old thread but perhaps the answer is the use of Indirection:${!PARAMETER}
For e.g., consider the following lines:
Though this is a very old thread, this device is ideal for either directly or randomly selecting a file/directory for processing (playing tunes, picking a film to watch or book to read, etc).
In bash I believe it is generally true that you cannot directly nest any two expansions of the same type, but if you can separate them with some different kind of expansion, it can be done.
Explanation: e is an array of directory names, c the selected directory, either named explicitly as $2,
where ... is the alternative random selection given by
where the
number generated by bash is divided by the number of items in array e, given by
yielding the remainder (from the % operator) that becomes the index to array e
Thus you have four nested expansions.