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.

  1. I¡¯m no longer positive where you are getting your information, but great topic. I needs to spend some time studying much more or working out more. Thank you for wonderful information I used to be looking for this information for my mission.

  2. fais ce que tu estimer faire, mais sans t’endetter.

  3. I enjoyed reading this a lot… I really hope to read much more of your posts in the future, so I’ve bookmarked your blog. But I couldn’t just bookmark it, oh no.. When I see quality website’s like this one, I like to share it with other people So I’ve designed a backlink to your site (from …

  4. excellent points altogether, you simply won a logo new reader. What could you recommend in regards to your put up that you made some days in the past? Any sure?

  5. This would be the right weblog for anybody who wishes to discover this subject. You realize a fantastic deal of its almost tough to argue along with you (not too When i would want…HaHa). You really put a brand new spin utilizing a topic thats been written about for decades. Great stuff, just fantastic!

  6. Oh my goodness! a tremendous article dude. Thanks Nevertheless I’m experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting equivalent rss problem? Anyone who knows kindly respond. Thnkx

  7. Deference to author , some good entropy. “When anyone stop to be able to dream a person stop to reside.Inches through Malcolm Stevenson Forbes.|

  8. I cannot thank you more than enough for the blogposts on your website. I know you’d put a lot of time and energy into them and truly hope you know how considerably I appreciate it. I hope I could do the same for another person at some time.

  9. Hey, sehr professioneller Eintrag, den du da gepostet hast. Wenn du etwas komparabel professionelles durchlesen möchtest, würde ich, wenn ich du wäre, dringend diese Seite besuchen.

  10. It is rare to encounter a professional in whom you will surely have some faith. In the world in the present day, nobody definitely cares about showing others the way out in this issue. How fortuitous I am to have actually found a really wonderful web-site as this. It is people like you that make a true difference in this world through the suggestions they write about.

  11. Hey there, You have performed an admirable job. I’ll absolutely stumbleupon this as well as in our view recommend to my buddies. I know they’ll be taken advantage of this web site.|

  12. Can I just say what a aid to find someone who truly is aware of what theyre speaking about on the internet. You positively know the way to carry a problem to mild and make it important. More individuals need to learn this and understand this side of the story. I cant believe youre not more in style since you undoubtedly have the gift.

  13. I am unable to thank you adequately for the posts on your web page. I know you placed a lot of time and effort into these and truly hope you know how considerably I appreciate it. I hope I am able to do a similar thing for someone else sooner or later.

  14. You produced some decent points there. I looked on-line to the problem and discovered most individuals will go along with along together with your internet website. 307112

  15. Nice blog post! It\\\’s so difficult to acquire good information on Internet Marketing on the net nowadays with the fluff that is on the market. I expect reading more within your articles and cultivate the wonderful work.

  16. My partner and i’ve got to say, through your research via several weblogs every single week, the authentic topic of your respective website is without a doubt distinguishable (to your acceptable variables). Except for those who mind people requesting, what would be the model for that look and could it function as custom-devised extramarital relationship? It genuinely is superior than the actual themes My partner and i take benefit of for some my weblogs

  17. Many thanks for your time to have put these things together on this web site. Michael and I very much valued your suggestions through the articles on certain things. I am aware that you have many demands on your own schedule therefore the fact that a person like you took equally as much time just like you did to guide people really like us by way of this article is even highly loved.

  18. There is visibly a bunch to know about this. I believe you made various great points in features also.

  19. If you are procuring in a warm place, then an undercover mall with some excellent air conditioning will make your excursion a good deal far more nice. Forget a mall purchasing middle that is cramped and badly laid out this form of environment will motivate you into rush acquiring and you will regret the purchases you make in the prolonged run.

  20. I couldn’t have really asked for a much better blog. You happen to be always available to provide excellent guidance, going right to the point for easy understanding of your subscribers. You’re truly a terrific professional in this arena. Thanks a lot for always being there for folks like me.

  21. What would most people do without the wonderful concepts you discuss on this website? Who has got the persistence to deal with essential topics for the sake of common subscribers like me? My partner and i and my buddies are very lucky to have your web site among the ones we often visit. It is hoped you know how very much we enjoy your hard work! Best wishes through us all.

  22. Your site handed superb specifics in the following. I personally crafted a evaluation on the subject determined nearly any folks may possibly realize your website.

  23. Thanks for the marvelous posting! I definitely enjoyed reading it, you might be a great author.I will ensure that I bookmark your blog and will eventually come back down the road. I want to encourage you to continue your great work, have a nice weekend!

  24. I have seen that currently, more and more people are now being attracted to cams and the subject of taking pictures. However, as a photographer, you need to first invest so much time frame deciding the exact model of video camera to buy in addition to moving from store to store just so you could possibly buy the most inexpensive camera of the brand you have decided to pick out. But it does not end just there. You also have to think about whether you should obtain a digital digicam extended warranty. Thanks alot : ) for the good ideas I received from your blog.

  25. I really aspire to receive wholly understood it what been recently made sure

  26. Thank you for all your valuable hard work on this site. Betty takes pleasure in engaging in research and it is truly obvious why. I learn all relating to the lively way you render rewarding items on this blog and even improve contribution from the others on this concept so my princess is without a doubt starting to learn plenty of things. Take pleasure inside the rest of the year. You might have been performing a dazzling job.

  27. The suggestions you contributed here are very priceless. It turned out such a pleasurable surprise to get that awaiting me immediately i woke up today. They are usually to the point as well as simple to grasp. Warm regards for the useful ideas you’ve shared here.

  28. With havin so much content and articles do you ever run into any issues of plagorism or copyright violation? My website has a lot of exclusive content I’ve either authored myself or outsourced but it seems a lot of it is popping it up all over the internet without my agreement. Do you know any methods to help stop content from being stolen? I’d genuinely appreciate it.

  29. Good post. I study something more challenging on completely different blogs everyday. It can all the time be stimulating to read content from other writers and follow slightly one thing from their store. I’d choose to make use of some with the content on my blog whether you don’t mind. Natually I’ll offer you a link in your web blog. Thanks for sharing.

  30. Some tips i have usually told people is that while looking for a good on the net electronics retail outlet, there are a few factors that you have to remember to consider. First and foremost, you should make sure to discover a reputable and reliable shop that has picked up great critiques and ratings from other consumers and business world professionals. This will ensure you are getting along with a well-known store that provides good assistance and assistance to its patrons. Many thanks for sharing your thinking on this weblog.

  31. Normally I don’t read post on blogs, however I wish to say that this write-up very forced me to check out and do so! Your writing style has been surprised me. Thank you, quite nice post.

  32. It is really rare to get a professional in whom you will surely have some trust. In the world nowadays, nobody actually cares about showing others the way out in this subjecttopic. How fortunate I am to have definitely found such a wonderful blog as this. It is really people like you who really make a true difference in this world through the suggestions they write about.

  33. Il vivre amendement de nous en meubler nous-même de sorte appliquer mais insister à manier avec le feu à l’exemple de ça, continuez, on en reparle dans pour années.en 2005, la accort a été dépouiller par l’ouragan et guitoune de allonger à vivre tant bien que mal. Une bouton de porte de musiciens, des associé d’un ensemble de jazz, s’efforce de rafraîchir son entourage de vie et, absolument la alentours de ce douloureux événement, ordonner d’organiser une brio traditionnelle. Au Sein De les massacreur un tromboniste de talent, a bien du mal à augmenter à se contenter de son existence. Il lot sa vie avec sa compagnie et leur bébé mais secrètement, le massacreur n’aspire qu’à une événement : il loucher renouer avec son antédiluvien épouse

  34. Could not thank you fully for the articles on your web page. I know you set a lot of time and effort into these and truly hope you know how deeply I enjoy it. I hope I will do something identical for someone else sooner or later.

  35. The tips you provided here are quite valuable. It turned out such an enjoyable surprise to have that awaiting me as i woke up now. They are continually to the point and simple to grasp. Thanks for your time for the clever ideas you’ve shared above.

  36. An engrossing communication is worth note. I guess that you should make many on this topic, it power not be a bias topic but generally people are not enough to utter on much topics.

  37. Thank you your post, previously it was interesting and compelling. I discovered my way here through Google, I am going to come back once more :)

  38. The guidelines you discussed here are really useful. Rrt had been such a pleasurable surprise to get that awaiting me immediately i woke up today. They are continually to the point plus easy to interpret. Thank you so much for the useful ideas you’ve got shared above.

  39. I like the valuable information you provide in your articles. I will bookmark your blog and check again here regularly. I’m quite sure I will learn a lot of new stuff right here! Good luck for the next!

  40. It’s actually a cool and helpful piece of info. I’m happy that you shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

  41. Thanks for all your valuable efforts on this website. Debby really loves carrying out investigations and it’s really simple to grasp why. Most people learn all relating to the compelling ways you convey invaluable secrets by means of this web site and therefore encourage participation from other people about this idea and our own daughter is always becoming educated a whole lot. Have fun with the rest of the new year. You’re the one conducting a useful job.

  42. Thanks a lot for your time and efforts to have put these things together on this weblog. Josh and i also very much liked your suggestions through your own articles in certain things. I know that you have many demands on your schedule therefore the fact that you took the maximum amount of time just like you did to steer people like us through this article is even highly treasured.

  43. Say thanks a lot for your time and effort to have put these things together on this blog site. Janet and i also very much prized your knowledge through your own articles about certain things. I recognize that you have many demands with your schedule so the fact that a person like you took all the time just like you did to steer people like us through this article is also highly prized.

  44. I like the useful information you give within your articles. I?ll bookmark your blog and check again here regularly. I’m quite confident I?ll learn a great deal of new stuff right hear Greatest of luck for the next!

  45. I simply want to mention I am just new to blogs and seriously enjoyed your blog site. Very likely I’m want to bookmark your blog . You definitely have amazing posts. Thanks a lot for revealing your webpage.

  46. dan uggla contract

  47. What would most people do without the amazing thoughts you share on this web site? Who has got the endurance to deal with essential topics in the interests of common subscribers like me? I actually and my buddies are very fortunate to have your web site among the ones we often visit. Hopefully you know how much we value your efforts! Best wishes coming from us all.

  48. Monaco V4 Tag Heuer

  49. All I can point out is, I am not sure what to say! Except certainly, for the great tips which have been shared on this blog. I am able to think of a trillion fun ways to read the content articles on this site. There’s no doubt that I will ultimately take action utilizing your tips on those issues I could never have been able to deal with alone. You’re so thoughtful to allow me to be one of those to learn from your practical information. Please know how much I enjoy the whole thing.

  50. It looks like your enjoying posting things like this

  1. No trackbacks yet.