Friday, May 3, 2024
19
rated 0 times [  19] [ 0]  / answers: 1 / hits: 958  / 2 Years ago, thu, october 6, 2022, 2:53:34

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?


More From » command-line

 Answers
4

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.


[#25254] Thursday, October 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
looweets

Total Points: 52
Total Questions: 114
Total Answers: 111

Location: Turkmenistan
Member since Sat, Apr 16, 2022
2 Years ago
looweets questions
Fri, Apr 29, 22, 09:47, 2 Years ago
Thu, Nov 3, 22, 03:32, 2 Years ago
Sun, Mar 5, 23, 00:43, 1 Year ago
;