Run last command with all the letters transformed to lowercase

Solution 1:

You can accomplish that by adding the following small function (I call it ?? to be very simple, but you can change this as you wish) in your .bashrc file:

?? () { "${@,,}"; }

Next, when you will run:

?? !!

the last command will be run with all the letters to lowercase.

Explanation

  • !! : this is part of bash's history tools, specifically, it expands to the last command run. So, for example, echo !! will print out the last command run in this particular shell session.
  • "${@,,} : The construct ${foo,,} will convert the contents of the variable $foo converted to all lowercase letters. $@ is the array of positional parameters. When used inside a function, it is expanded to the parameters passed to that function.

So, "${@,,}" will simply convert whatever was passed to the function into lowercase and then execute it. Therefore, running the ?? function with !! as a parameter will convert the last command to lowercase and run it.

Solution 2:

`echo  !! | tr '[:upper:]' '[:lower:]'`

The key is in the ` (backticks) quotations - which runs the output as a command.

Solution 3:

here comes a programmers answer.... use python:

`echo "print '!!'.lower()" | python`

Or Ruby:

`echo "print '!!'.downcase" | ruby`

Or Perl (probably my favorite):

`perl -e "print lc('!!');"`

Or PHP:

`php -r "print strtolower('!!');"`

Or Awk:

`echo "!!" | awk '{ print tolower($1) }'`

Or Sed:

`echo "!!" | sed 's/./\L&/g'`

Or Bash:

str="!!" ; ${str,,}

Or zsh

echo "A='!!' ; \$A:l" | zsh

Or NodeJS if you have it:

`echo "console.log('!!'.toLowerCase());" | node`

You could also use dd (but I wouldn't!):

 `echo "!!" | dd  conv=lcase 2> /dev/null`

You could also create a small script to do the job:

sudo bash -c 'cat << EOF > /usr/local/bin/lower
#!/usr/bin/python
import fileinput
print "".join(fileinput.input()).lower(),
EOF' && sudo chmod +x /usr/local/bin/lower

Which you use like so:

`echo !! | lower`