{"id":34,"date":"2011-07-19T11:49:11","date_gmt":"2011-07-19T11:49:11","guid":{"rendered":"http:\/\/cpscotti.com\/blog\/?p=34"},"modified":"2011-07-19T11:49:11","modified_gmt":"2011-07-19T11:49:11","slug":"bash-history-history-expansion-and-that-commandargs-you-aways-forget","status":"publish","type":"post","link":"http:\/\/cpscotti.com\/blog\/?p=34","title":{"rendered":"Bash History, History Expansion and that command+args you aways forget"},"content":{"rendered":"<p>Since I&#8217;m taking this blog thing a lot more serious now, I&#8217;ll devote a quick blog post to one of the things I love more about the <em>Bourn Again<\/em> SHell: history.<\/p>\n<p>Working in a Linux environment inevitably makes you use a shell (i.e. the actual brain behind the famous &#8220;temrinal&#8221;). There are many shells one could use but I&#8217;ll risk saying bash is the most common. In all fairness, if you are any sort of coder\/developer\/debugger\/hacker\/script-kiddie\/indiana-jones-of-the-computer-world you&#8217;ll eventually fall in love with it. It&#8217;s also worth mentioning that most people (me included), they never &#8220;sat down&#8221; to learn bash. We just started copy&amp;pasting commands we saw somewhere, then you start reading whatever you&#8217;re posting, then you learn to use <em>manpages<\/em> (or equivalent), etc.. 5 years later you certainly &#8220;know your ways&#8221;.<\/p>\n<p>For all bash\/shell related problems\/questions, there are probably 5 different ways around it. That said, it&#8217;s common to stick always to the same tools\/methods for solving all problems&#8230;<\/p>\n<blockquote><p>&#8220;this way works, so why learning anything new?&#8221;<\/p><\/blockquote>\n<p>Basically, Time! The more culturally aware you are in terms of bash &amp; bash scripting; the quicker you&#8217;ll solve your problems.<\/p>\n<p>Finally, after all this obvious blogish talk, let&#8217;s get to the point:<\/p>\n<p>[sourcecode language=&#8221;shell&#8221;]<br \/>\nhistory<br \/>\n[\/sourcecode]<\/p>\n<p>That command shows all the previous commands you (your user) executed. Ages ago I learned to use it with grep to see what command I had executed that had *string* on it.<\/p>\n<p>[sourcecode language=&#8221;shell&#8221;]<br \/>\nhistory | grep g++<br \/>\n[\/sourcecode]<\/p>\n<p>This one&#8217;s really useful and if that&#8217;s new to you, this post is already worth it&#8217;s effort. But that one&#8217;s clumsy,<br \/>\nyou&#8217;ll probably end up copying &amp; pasting something by using that filthy rat of yours or by petting that flat life-less thing on<br \/>\nyour notebook &#8211; that&#8217;s not efficient.<\/p>\n<p>By running that command you&#8217;ll see that there&#8217;s an index for each command executed; for example:<\/p>\n<p>[sourcecode language=&#8221;shell&#8221;]<br \/>\n#lol.. those are the oldest entries on my history (5k commands long)<br \/>\ncpscotti@clovis-laptop:~$ history<br \/>\n1\u00a0 cd push-snowboarding\/<br \/>\n2\u00a0 git status -s<br \/>\n3\u00a0 cd PushBurton2<br \/>\n4\u00a0 ls<br \/>\n5\u00a0 git status -s<br \/>\n6\u00a0 git add *.cpp *.h<br \/>\n&#8230;<br \/>\n[\/sourcecode]<\/p>\n<p>That index is there for a very nice reason; History Expansion. By entering !index, you&#8217;ll run that line again. Suppose I want to<br \/>\nrun an old command again ( e.g. meego-sb-session start ) which is hard to remember or too long to type, one can just find out the<br \/>\nindex for the last occurrence of that command and.. voila!<\/p>\n<p>[sourcecode language=&#8221;shell&#8221;]<br \/>\n[sbox-HARMATTAN_X86: ~] &gt; history | grep start<br \/>\n167\u00a0 history | grep start<br \/>\n168\u00a0 meego-sb-session start<br \/>\n501\u00a0 meego-sb-session start<br \/>\n503\u00a0 history | grep start<br \/>\n[sbox-HARMATTAN_X86: ~] &gt; !168<br \/>\nmeego-sb-session start<br \/>\n&#8230;<br \/>\n[\/sourcecode]<\/p>\n<p>But hey, if you are still lazy to type in those two lines.. we can go further!<br \/>\nThere&#8217;s another command I execute quite often that&#8217;s just a PITA to remembering\/type..:<\/p>\n<p>[sourcecode language=&#8221;shell&#8221;]<br \/>\ncpscotti@clovis-laptop:~$ Xephyr :2 -host-cursor -screen 854x480x16 -dpi 96 -ac +extension Composite &amp;<br \/>\n[\/sourcecode]<\/p>\n<p>Diving a bit more into <em>history<\/em>&#8216;s <a title=\"manpages\" href=\"http:\/\/linux.die.net\/man\/3\/history\">manpages<\/a> you&#8217;d find the &#8221; ? &#8220;and a lot more!<br \/>\nI won&#8217;t explain much, just watch! \ud83d\ude00<\/p>\n<p>[sourcecode language=&#8221;shell&#8221;]<br \/>\ncpscotti@clovis-laptop:~$ !?Xep?<br \/>\nXephyr :2 -host-cursor -screen 854x480x16 -dpi 96 -ac +extension Composite &amp;<br \/>\n[\/sourcecode]<\/p>\n<p>Of course you have to &#8220;know your past&#8221; otherwise you&#8217;ll end up executing bizarre commands but well.. that works great for me!<\/p>\n<p>Enjoy!<\/p>\n<p>&nbsp;<\/p>\n<p>Oh, one last thing! You can &#8220;pimp up&#8221; your history&#8217;s size! I think Ubuntu&#8217;s default is 2000.. way to small in my opinion! Just edit HISTSIZE inside your .bashrc script!<\/p>\n<p>(in doubt check manpages for bash and history!)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I&#8217;m taking this blog thing a lot more serious now, I&#8217;ll devote a quick blog post to one of the things I love more about the Bourn Again SHell: history. Working in a Linux environment inevitably makes you use a shell (i.e. the actual brain behind the famous &#8220;temrinal&#8221;). There are many shells one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12,8],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-bash","category-gnu","category-linux"],"_links":{"self":[{"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":4,"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":47,"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions\/47"}],"wp:attachment":[{"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cpscotti.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}