Getting the bang (!) for your buck ($) from BASH

Most of the work I do is on systems who’s primary interface is BASH. BASH is the shell that runs on most Linux distributions and Mac OS X when you open a terminal or SSH in. There are a few short cuts I have picked up along the way that are built into BASH but aren’t all that obvious or in the man ( manual ) pages. One very powerful shortcut, ! ( refered to as bang ), is used in BASH to reference the prior command(s) in varius ways and can really speed up your work in the terminal.

!$
The command that this post is named for is used to reference the last argument in the previous command. For example, let’s say you set up a new directory for your client you would need to set the ownership and permissions on it.

mkdir /dir/made/for/client1
chown client1:client1 /dir/made/for/client1
chmod 750 /dir/made/for/client1

This can be shortened by using the !$ shortcut like this.

mkdir /dir/made/for/client1
chown client1:client1 !$
chmod 750 !$

The time savings here are minor but over time can help, by referencing the first command we also reduce the number of typos.

!!
This command references the complete previous command. “Doesn’t the up arrow do that?” you ask? The up arrow pulls the previous command up, allowing you to run it again or modify it. !! allows you to insert the previous command into what you are typing. Take the previous example, if you didn’t have permission to make the directory the first time you tried you could try it again using sudo, like this.

mkdir /dir/made/for/client1 (returns an error about permission denied)
sudo mkdir /dir/made/for/client1 (works)

This can be shortened using the !! shortcut like this.

mkdir /dir/made/for/client1 (returns an error about permission denied)
sudo !! (works)

!number or !-number
BASH, in most configurations, keeps a history of your commands. You can see a list of those commands using the command history.

server01:~ j2$ history
26 command1
27 command2
28 command3
29 command4
30 command5
31 command6
32 command7
33 command8
34 command9
35 history

You can reference any of these commands using the number next to them and the ! shortcut. If I wanted to run command5 again I would do this.

!30

You can also reference commands in reverse by negating them. If I remembered that I ran command5 six commands ago I could do this.

!-6

!:number
By putting a : and a number after the ! you can reference individual arguments in a previous command. Each “word” in a command gets assigned a number, starting with 0. For example, in the command chmod 750 /dir/made/for/client1 chmod = 0, 750 = 1, and /dir/made/for/client1 = 2. So if I had to change the permissions on a few directories it would look like this.

chmod 750 /dir/made/for/client1
chmod 750 /dir/made/for/client2
chmod 750 /dir/made/for/client3

Using the shortcut it would look like this.

chmod 750 /dir/made/for/client1
!:0 !:1 /dir/made/for/client2
!:0 !:1 /dir/made/for/client3

Finally, the last shortcut and this one can be combined to reference specific parts of a command in your history list. Say I just did all those chmod commands above but forgot to chown client1′s dir. Using the combination I could do this.

chown client1:client1 !-3:2

You can now see why ! is one of my favorite tools while working in BASH. If you have any questions or know of another way to use !, please leave a comment.

    • Easy Rider Car Extra Small Harness For Dogs Black
    • March 10th, 2012

    I would like to thnkx for the efforts you’ve put in writing this website. I’m hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has encouraged me to get my own website now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

  1. Oh my goodness! Amazing article dude! Thank you so much, However I am having issues with your RSS. I don’t know the reason why I am unable to join it. Is there anyone else having similar RSS issues? Anybody who knows the answer will you kindly respond? Thanx!!

  2. Wow! This could be one particular of the very helpful blogs We’ve ever arrive across within this subject. Basically Excellent. I’m also an expert in this topic therefore I can understand your effort.

  3. Oh boy! It is like you read my mind! You seem to know so much about this, just like you wrote the book in it or something. I think that you can do with some pics to drive the content home a bit, besides that, this is outstanding blog post. A good read. I will definitely return again.

  4. Btw, Gary Johnson’s pro-choice on abortion, he’s in favor of gay marriage and he wants to legalize drugs. Gary Johnson makes Obama look like Pat Robertson on social issues. Obama’s only pro-choice on abortion, against gay marriage and against legalizing drugs. Gary Johnson’s more socially liberal than any Democrat in congress.

  5. Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing. Do you have any points for newbie blog writers? I’d genuinely appreciate it.

  6. Several really nice stuff on this website, I like it.

  7. I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to design my own blog and would like to find out where u got this from. thank you

  8. Spot on with this write-up, I actually think this web site wants far more consideration. I’ll probably be once more to learn rather more, thanks for that info.

  9. I have been exploring for a little bit for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this information So i’m happy to convey that I have a very good uncanny feeling I discovered exactly what I needed. I most certainly will make sure to do not forget this site and give it a glance regularly.

  10. The other day, while I was at work, my sister stole my iphone and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!

  11. Excellent, terrific knowledge.

  12. Hey! I know this is kinda off topic however , I’d figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog article or vice-versa? My site goes over a lot of the same subjects as yours and I think we could greatly benefit from each other. If you are interested feel free to shoot me an e-mail. I look forward to hearing from you! Excellent blog by the way!

  13. Simply wish to say your article is as amazing. The clarity to your put up is just spectacular and that i can assume you’re a professional on this subject. Fine together with your permission let me to clutch your feed to keep updated with imminent post. Thanks a million and please carry on the rewarding work.

  14. Nice read, I just passed this onto a colleague who was doing some research on that. And he just bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! “Too much sanity may be madness. And maddest of all, to see life as it is and not as it should be” by Miguel de Cervantes.

  15. I’m impressed, I have to say. Actually not often do I encounter a weblog that’s both educative and entertaining, and let me inform you, you’ve hit the nail on the head. Your idea is outstanding; the difficulty is something that not sufficient persons are speaking intelligently about. I am very completely happy that I stumbled across this in my seek for one thing relating to this.

  16. Rattling wonderful information can be found on this site.

  17. you have got an ideal blog here! would you prefer to make some invite posts on my blog?

  18. I needed to thank you for this good read!! I absolutely loved every bit of it. I have you saved as a favorite to check out new stuff you post…

  19. I am unquestionably not as well familiar with this subject but I do wish to explore blogs for layout suggestions and fascinating topics. You very described a that I almost always don’t care considerably about and designed it incredibly fascinating. This is a wonderful weblog that I will take note of. review about info at our site

  20. I’m definitely not too acquainted with this matter but I do like to explore blogs for layout suggestions and interesting subjects. You in fact described a that I frequently don’t care a good deal about and built it extremely fascinating. That is a good weblog that I will pay attention to. get info at our site

  21. I am actually not too acquainted with this subject but I do prefer to visit blogs for layout suggestions and exciting subjects. You essentially described a that I generally really do not care a great deal about and created it fairly fascinating. This can be a great blog that I will pay attention to. Find out information at our site

  22. I’m pretty pleased to discover this site. I need to to thank you for your time due to this fantastic read!! I definitely liked every part of it and I have you bookmarked to see new things in your blog.

  23. I am realistically not too familiar with this topic but I do prefer to take a look at blogs for layout suggestions and intriguing subjects. You actually described a that I in most cases really don’t care substantially about and built it fairly fascinating. That is a nice blog that I will take note of. download Guide at site

  24. I am really not as well familiar with this subject but I do wish to explore blogs for layout recommendations and exciting topics. You essentially described a that I often really don’t care a lot about and crafted it particularly fascinating. This is a good weblog that I will take note of. Find out facts on our website

  25. I am surely not as well acquainted with this subject but I do wish to stop by blogs for layout ideas and intriguing subjects. You surely described a that I almost always do not care a lot about and made it pretty fascinating. This is a great blog that I will be aware of. download info at website

  26. Observed your short article incredibly remarkable certainly. I seriously really enjoyed looking at it and you make fairly some excellent factors. I will bookmark this site with the upcoming! Relly good article.

  27. I used to be more than happy to find this internet-site.I wanted to thanks to your time for this excellent read!! I definitely enjoying each little bit of it and I’ve you bookmarked to check out new stuff you blog post.

  28. Great weblog man! I merely added this short article to my own Twitter account. Keep it up!

  29. Hello presently there, I discovered your site using Google at the same time as looking for a related matter, your internet site got below up, it looks like it’s good. I get bookmarked it in my google bookmarking.

  30. Khmer Karaoke Celebrities We was suggested this web site by my cousin. I’m unsure whether this post is compiled by him as nobody else recognize such detailed about my own problem. You are usually wonderful! Thanks! your write-up about Khmer Karaoke Megastars Sincerely Lawrence Craig

  31. Hmm it seems like your website ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to everything. Do you have any suggestions for rookie blog writers? I’d certainly appreciate it.

  32. Sweet, thx!

  33. Thanks for your strategies. One thing really noticed is banks plus financial institutions really know the spending practices of consumers and also understand that most of the people max out their real credit cards around the holiday seasons. They smartly take advantage of this kind of fact and start flooding your inbox plus snail-mail box along with hundreds of no-interest APR credit cards offers immediately after the holiday season closes. Knowing that when you are like 98% of American public, you’ll jump at the opportunity to consolidate card debt and transfer balances for 0 APR credit cards.

  34. WOW! What a fantastic looking site! Thanks for all you do to make Huntington Beach a beautiful place!

  35. I have got read several perfect stuff here. Definitely price bookmarking pertaining to revisiting. I surprise what sort of lot effort you put to make this kind of wonderful useful website.

  36. I bring on about part to your weblog on my friends call for the sake of and when i start to reading it i cannot end. Your article is so meet, detonation in it comprise so much true. Is there any more distribution you striking your content? Humour, afflict with my sign.

  37. Excellent beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog web site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear concept

  38. Greetings! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone. I’m trying to find a theme or plugin that might be able to fix this issue. If you have any suggestions, please share. Thanks!

  39. It¡¦s truly a great and helpful piece of info. I¡¦m happy that you shared this useful information with us. Please stay us informed like this. Thanks for sharing.

  40. Well I sincerely enjoyed reading it. This information procured by you is very helpful for good planning.

  41. Have you tried SEOPressor? It’s easily the very best SEO wordpress plugin. I utilize it on my sites and also I’m now getting a huge selection of customers every day to my sites it’s fantastic!

  42. There is certainly apparently a lot to identify in regards to this. I assume you’ve made certain good points in features also.

  43. This site is awesome, study a handful of content articles and loved them all, thanks OP.

  44. Observed your report pretty appealing without a doubt. I actually experienced examining it and also you make really some excellent points. I’ll bookmark this web site to the foreseeable future! Relly good content.

1 ... 3 4 5
  1. No trackbacks yet.