There is 1 line to resolve the original OP question, the base name of the script with a file extension:
progname=$(tmp=${0%.*} ; echo ${tmp##*/})
Here's another, but using the cheat for the base name:
progname=$(basename ${0%.*})
Other answers moved away from the original OP question and focused on whether it is possible to simply extend the result of expressions with ${!var} but faced with the limitation that var must explicitly match the variable name. Having said that, nothing will stop you from getting a 1-line answer if you combine the expressions with a semicolon.
ANIMAL=CAT BABYCAT=KITTEN tmp=BABY${ANIMAL} ; ANSWER=${!tmp} # ANSWER=KITTEN
If you want this to look like a separate statement, you can nest it in a subshell, i.e.
ANSWER=$( tmp=BABY${ANIMAL) ; echo ${!tmp} )
An interesting use is indirect work on the arguments to the bash function. You can then nest your bash function calls to achieve multi-level nested indirection, because we are allowed to do nested commands:
Here is a demonstration of the indirectness of the expression:
deref() { echo ${!1} ; } ANIMAL=CAT BABYCAT=KITTEN deref BABY${ANIMAL}
Here is a demonstration of multi-level indirection through nested commands:
deref() { echo ${!1} ; } export AA=BB export BB=CC export CC=Hiya deref AA
Stephen Quan Apr 24 '17 at 2:29 on 2017-04-24 02:29
source share