I want to set a bash variable to the output of a command and ensure it is all lowercase. I want to keep my code as clean as possible and not pipe output anywhere.
This is what I had before:
host=$(hostname -s | tr '[:upper:]' '[:lower:]')
Instead I am now using two definitions (being slightly shorter):
host=$(hostname -s); host=${host,,}
but I'm wondering if it's possible to do it in just one definition for host without piping to tr (or anything else). Can the second line be combined any further?
Edit: This is exactly why I should copy/paste my code. I did in fact have '[:upper:]' and '[:lower:]' in my script but typed it incorrectly.
host=$(hostname -s | tr [:upper:] [:lower:])is bad because thetrcommand will get messed up if it is run in a directory that contains a file whose name is a single character:,e,l,o,p,r,u, orw.host=$(hostname -s | tr '[:upper:]' '[:lower:]')is good. See the "Ranges" section in glob - Greg's Wiki for details of why the quotes are necessary.local result=$(someCommand)always reports true becauselocalsucceeds, even ifsomeCommandfails.