Help Requested with bash Parameter Substitution
I just finished reading a fascinating thread about a parameter substitution problem in bash titled "Yet Another Bash Quotes Within Quotes Issue".
I'm new to learning Unix/Linux (and this group) and am working on a simple script. Filing this thread under "Obscure Scripting Features that May Bite You on the Ass" I read through for future reference.
The problem involved getting a script to generate an e-mail message titled "Error from daily_file_backup" to root. This person wanted to use:
MAIL="/bin/mailx -s Error from $(basename $0) root"
/bin/umount /data_backup||{ echo "backup unmount failed"|$MAIL;exit 1;
with quotes around "Error from $(basename $0)" so that $0 would expand into daily_file_backup.
After a lot of back and forth, the solution was to use:
basename ${0/-//}
or "basename ${0/-//}", it isn't clear which.
Then the thread died without an explanation why this would work!!!!!!!!!!!!!!!!!!!!!!!!!
The user was happy, but WTF? I've tried all kinds of bash tutorials, but this type of construction doesn't appear, except that "/" and "//" are used for substring replacement. That's as far as I get.
Rather that posting this to the original thread--which appears to have died quite a few months ago--I'm posting this new thread.
|