Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Many times I have run a command with all the letters in uppercase (maybe because CapsLock was on), and I pressed Enter before realising this.

How can I run the last command with all the letters transformed to lowercase?

share|improve this question
    
Shame you're not using zsh. !!:l. –  Kevin May 13 at 20:13

4 Answers 4

up vote 19 down vote accepted

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.

share|improve this answer
7  
Perhaps you could add an explanation of how this works? It is very cryptic the way you've written it. Also, it doesn't actually work, as such. As you've written it, it will only work correctly if your PWD is empty. You need to quote the "!!". –  terdon May 13 at 13:34
1  
@terdon Now, thanks to glenn, it should work in any case... And there is nothing too much to explain: the function ?? converts its arguments letters to lowercaes and !! re-write automaticaly the previous command in the current command (so ?? takes as argument the previous command). –  Radu Rădeanu May 13 at 17:13
`echo  !! | tr '[:upper:]' '[:lower:]'`

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

share|improve this answer
2  
The backticks where invisible in the first version of the answer, due to the markup language. No need to downvote (without comment) for this; just edit. –  Rmano May 13 at 15:36
    
tr would be my first choice, too. Also works (alternative to backticks and without the POSIX bracket-classes) : $( echo !! | tr '[A-Z]' '[a-z]') –  belacqua May 27 at 21:23

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`
share|improve this answer

And the one with sed,

$(echo !! | sed 's/^\(.*\)$/\L\1/g')

Example:

$ SUDO APT-GET UPDATE
SUDO: command not found
$ $(echo !! | sed 's/^\(.*\)$/\L\1/g')

Output:

$(echo SUDO APT-GET UPDATE | sed 's/^\(.*\)$/\L\1/g')
[sudo] password for avinash: 
0% [Waiting for headers] [Waiting for headers] [Waiting for headers] [Connectin^
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.