Friday, January 24, 2014

Invert the case of a bash variable (bash scripting, ie, THIS IS A test -to- this is a TEST)

${V~~}, the (undocumented?) invert case expansion??


I stumbled upon this by accident, my bad memory is to blame for it, but it seems to be undocumented so I thought I would blog it out since nobody seems to have done so.

Under bash 3.x, I was trying to convert a path to lowercase which is done by ${VAR,,} but accidently used ${VAR~~} note: the worm characters, and got this interesting result (command results are in bold):


localhost ~$ TEST="THIS IS A test string"
localhost ~$ echo ${TEST~~}
this is a TEST STRING
localhost ~$ echo ${TEST,,}
this is a test string
localhost ~$ echo ${TEST^^}
THIS IS A TEST STRING
localhost ~$_


I went back to the man page, a few stack-exchange sites that I frequent, and found no trace, not even in Google, of this odd extra. I'm wondering if it is a remnant of POSIX compatibility forgotten, or did the documentation accidentally leave it out or forget about it? Either way, it is good to know, and now you know too. If I get around to it, I'll have to contact the developer(s) responsible for the current upkeep of bash, which may be hard since it seems many projects are forked and may very well affect multiple projects under multiple distributions, even perhaps as far as crossing the Linux <-> BSD barrier. I would think from what I've seen this could be challenging so I am notoriously lazy and it could be 5 or 10 years before I do. Anyone interested in taking this up, let me know, I'm very interested to hear. I respond to comments on the blog, that's the best way I think, or write me a post on one of the Q&A sites I use often: AskUbuntu, StackOverflow, ServerFault or, SuperUser.

3 comments:

  1. Nice blog, here I had an opportunity to learn something new in my interested domain. I have an expectation about your future post so please keep updates.
    Inverters in chennai

    ReplyDelete
  2. Day by day I am getting new things and learn new concept through your blogs, I am feeling so confidants, and thanks for your informative blog keep your post as updated one...
    Inverter in chennai

    ReplyDelete
  3. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    Battery dealers in chennai |Exide battery |Exide inverter battery

    ReplyDelete