Download your favorite Linux distribution at LQ ISO.
Go Back > Blogs > invalidid
User Name


Rate this Entry

Did you know how to replace via the ^ command with bash?

Posted 03-12-2010 at 09:05 AM by invalidid
Updated 03-12-2010 at 09:11 AM by invalidid

Hello All!

At first, I want to ask one question about using the popular bash commandline interpreter. Did you know about the ^ command?

If yes, you are a really well expierenced bash-user. If not, you won't loose anything in reading this blog entry. The command ^ is used to substitude some text, typed the last time before you hit the enter key.

One example of how to use it, surely makes the usage clear:
Let's assume you have a directory called "~/temp". And because of your writing speed, you make a typing mistake like that:

user@machine ~/temp $ cd temo
-bash: cd: temo: No such file or directory
Now you can use the ^ command:

user@machine ~ $ ^o^p
cd temp
user@machine ~/temp $
Did you see the what it does?
It replaces all occurences of the first part between the two carets (^) with the part between the last caret and EOL.

This can be sometimes a powerful trick to navigate through directories.
For instance, if you have child directories, whose names aren't much different from their parents one, such in this case:
user@machine ~/temp $ cd temp1
user@machine ~/temp/temp1 $ ^1^2
cd temp2
user@machine ~/temp/temp1/temp2 $ ^2^3
cd temp3
user@machine ~/temp/temp1/temp2/temp3 $ ^3^4
cd temp4
user@machine ~/temp/temp1/temp2/temp3/temp4 $
Seems powerful, doesn't it?
I hope I could give you a short introduction into this advanced bash command. If you have any questions, improvements or anything else, feel free to post comments.
Have a nice day with it!
Posted in Tips and tricks
Views 1103 Comments 2
« Prev     Main     Next »
Total Comments 2


  1. Old Comment
    didn't know that, interesting. but imo in
    cd tmo
    it is faster to rewrite it entirely then use shift to make '^' char.
    I'm not lazy in fact and I don't like to get used to some hacks like that.
    Posted 03-12-2010 at 10:51 AM by Web31337 Web31337 is offline
    Updated 03-12-2010 at 10:53 AM by Web31337
  2. Old Comment
    No, I did not know that. Freaky!
    Posted 03-12-2010 at 05:21 PM by CoderMan CoderMan is offline


All times are GMT -5. The time now is 09:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration