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. Admiring the persistence you put into your website and in depth information you offer. It’s awesome to come across a blog every once in a while that isn’t the same old rehashed information. Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  2. christian louboutin bridget

  3. Say thanks a lot for your efforts to have put these things together on this blog. Jack and I very much liked your input through your articles in certain things. I am aware that you have a number of demands in your schedule hence the fact that you took all the time like you did to help people like us via this article is definitely highly treasured.

  4. We pretty much appreciate your website post. There are actually a multitude of tactics we could put it to very good use while using no effort in time and resources. Thank you very much regarding helping make this post respond to many problems we have encountered before now.

  5. I cannot thank you fully for the posts on your website. I know you add a lot of time and effort into these and really hope you know how considerably I appreciate it. I hope I am able to do the identical thing for someone else at some point.

  6. The tips you contributed here are really priceless. It turned out such an enjoyable surprise to get that waiting for me immediately i woke up today. They are often to the point as well as simple to grasp. Thanks a lot for the innovative ideas you have shared right here.

  7. Awesome blog! Do you have any tips for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally overwhelmed .. Any recommendations? Bless you!

  8. Gnarly post mate, keep the excellent work, just shared this with ma friendz

  9. I just wanted to tell you how much my partner and i appreciate all you’ve provided to help increase the value of the lives of individuals in this subject material. Through the articles, I have gone through just a beginner to a pro in the area. It’s truly a honor to your efforts. Thanks

  10. It was a excitement coming across your site last night. I got here right now hoping to come across interesting things. And I was not upset. Your ideas on new approaches on this topic were useful and a good help to us. Thank you for leaving out time to write out these things plus for sharing your thoughts.

  11. The ideas you shared here are really priceless. Rrt had been such an exciting surprise to get that awaiting me immediately i woke up now. They are continually to the point and straightforward to understand. Thank you very much for the valuable ideas you’ve got shared here.

  12. I couldn’t have really asked for a better blog. You are ever present to give excellent advice, going directly to the point for quick understanding of your site visitors. You’re undeniably a terrific expert in this subject matter. Thanks for always being there for folks like me.

  13. I can’t thank you enough for the discussions on your website. I know you set a lot of time and energy into these and really hope you know how considerably I appreciate it. I hope I will do the same for another individual someday.

  14. Tag Heuer Repairs

  15. We really appreciate your site post. There are actually hundreds of approaches we could put it to good use with the help of minimal effort on time and resources. Thank you really for helping have the post answer many issues we have come across before now.

  16. red christian louboutin pumps

  17. I merely wanted to inform you about how much I actually appreciate everything you’ve discussed to help enhance the lives of an individual in this subject material. Through your own articles, I have gone via just an inexperienced to an expert in the area. It truly is truly a tribute to your work. Thanks

  18. Have you gone to these places? Wow, you are a traveller after all, and a blog poster at night

  19. We highly appreciate your website post. You can find a multitude of methods we could put it to great use with the help of minimum effort on time and hard earned cash. Thank you very much pertaining to helping make the post give light to many questions we have come across before now.

  20. Throughout this grand pattern of things you’ll receive a B- for effort and hard work. Exactly where you actually lost us ended up being on all the particulars. You know, as the maxim goes, details make or break the argument.. And that couldn’t be more correct at this point. Having said that, permit me tell you precisely what did deliver the results. Your text is definitely highly powerful and this is probably the reason why I am taking the effort in order to comment. I do not make it a regular habit of doing that. 2nd, even though I can easily notice a jumps in reason you come up with, I am not convinced of exactly how you appear to connect the points that produce the actual conclusion. For right now I shall yield to your point but wish in the future you link your dots much better.

  21. It’s a shame you don’t have a donate button! I’d most certainly donate to this fantastic blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this website with my Facebook group. Talk soon!

  22. What would we all do devoid of the wonderful tips you write about on this site? Who else has the endurance to deal with vital topics in the interest of common readers like me? My partner and i and my friends are very happy to have your site among the ones we generally visit. We hope you know how considerably we get pleasure from your work! Best wishes coming from us all.

  23. I wanted to tell you how much my spouse and i appreciate almost everything you’ve provided to help improve the lives of individuals in this subject material. Through your articles, I’ve gone through just a beginner to a pro in the area. It can be truly a gratitude to your work. Thanks

  24. Just about all of whatever you claim is astonishingly precise and it makes me ponder the reason why I had not looked at this with this light previously. This particular article really did switch the light on for me personally as far as this issue goes. But at this time there is actually one particular position I am not too comfy with and while I make an effort to reconcile that with the actual main idea of your point, let me see just what the rest of the visitors have to say.Nicely done.

  25. I just wanted to let you know how much I appreciate every thing you’ve shared to help increase the value of the lives of folks in this theme. Through the articles, I have gone out of just a newcomer to a pro in the area. It can be truly a honor to your initiatives. Thanks

  26. I would This post all about Bob’s Article Daytime Couple of for that ‘Que Piste Good explicitly something i necessary!.

  27. I enjoy this internet site, will surely arrive back. Make confident you carry on writing high quality posts. 149033

  28. Hey there. I want to to inquire something

  29. Terrific post however , I was wanting to know if you could write a litte more on this subject? I’d be very grateful if you could elaborate a little bit more. Thanks!

  30. I must associated with voice that affection personal generosity for the purpose of consumers who need supervision recover joyful. Your incredible big determination to finding a new sales and profits presentation upon was unquestionably valuable and also from now on eco-friendly made it possible if anyone is just like me to reach their dreams. Your distinctive critical strategies denotes a great deal to myself proactive to buddies. Most useful is hoping for; anywhere from what can cause.

  31. I intended to compose you the little remark to finally thank you again regarding the pretty secrets you have documented on this website. It’s certainly strangely open-handed with you to present publicly just what a few individuals might have marketed for an e-book to get some dough for themselves, most importantly considering that you could possibly have tried it if you decided. Those smart ideas as well acted as a easy way to know that someone else have similar zeal the same as my own to realize a whole lot more concerning this condition. I’m certain there are millions of more pleasurable sessions up front for folks who looked at your blog.

  1. No trackbacks yet.