{"id":15,"date":"2016-05-26T13:34:57","date_gmt":"2016-05-26T13:34:57","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=15"},"modified":"2016-06-20T10:43:25","modified_gmt":"2016-06-20T10:43:25","slug":"bash-its-not-a-bug-its-a-feature","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/05\/26\/bash-its-not-a-bug-its-a-feature\/","title":{"rendered":"bash &#8211; its not a bug its a feature"},"content":{"rendered":"<blockquote>\n<h2><strong>It&#8217;s not a bug it&#8217;s a feature<\/strong><\/h2>\n<\/blockquote>\n<p>It has been said so many times it goes beyond\u00a0cliche. \u00a0It is really easy to try and explain away odd behaviors as some sort of esoteric feature<\/p>\n<p>It took a while before I caught up on the pattern. \u00a0I have a habit of copying text between windows, usually in the form of a command. \u00a0Putty makes this really easy, simply highlight the text in one window and right click in another.<\/p>\n<p>The reason I did the copy and paste in the first place was because the command was really long. \u00a0Imagine my surprise when I try and run it a second time only to discover it is not in my command history.<\/p>\n<p>If you start your command with one or more spaces, then it will usually fail to show up in the command history. \u00a0It is the variable HISTCONTROL that defines if command begins with a space it should be omitted. \u00a0This is done when the value is defined as <strong><em>ignorespace.<\/em><\/strong>\u00a0There are two other possible values for the HISTCONTROL variable, they are <em><strong>ignoredups<\/strong><\/em> and <em><strong>ignoreboth<\/strong><\/em>\u00a0&#8211; the <em><strong>ignoredups<\/strong><\/em> does exactly what it says. \u00a0If you do the exact same command multiple times the duplicates will be suppressed. \u00a0The ignoreboth option simply combines both <em><strong>ignoredups<\/strong><\/em> and <em><strong>ignorespace<\/strong><\/em>.<\/p>\n<p>The command history is stored in the file that is pointed to by the HISTFILE variable.<\/p>\n<div class=\"sbody-code\">\n<pre><code>HISTFILE=\/home\/dock\/.bash_history\r\n<\/code><\/pre>\n<\/div>\n<p>The history file isn&#8217;t without limit, the number of commands that will be stored is defined by the HISTFILESIZE variable.<\/p>\n<div class=\"sbody-code\">\n<pre><code>HISTFILESIZE=2000\r\n<\/code><\/pre>\n<\/div>\n<p>It is possible to see the last few commands in a few ways. \u00a0If the command is really recent, simply use the up and down arrow to scroll through the commands. \u00a0When you find the one you are interested it is already typed, just press enter. \u00a0To list the last commands simply\u00a0type history.<\/p>\n<div class=\"sbody-code\">\n<pre><code> 973 ls\r\n 974 ls -ltr\r\n 975 sync\r\n 976 cd \/\r\n 977 sync\r\n 978 sudo init 0\r\n 979 echo $HISTIGNORE\r\n 980 ls\r\n 981 histroy\r\n 982 history\r\n<\/code><\/pre>\n<\/div>\n<p>The history command will\u00a0display the last HISTSIZE commands<\/p>\n<div class=\"sbody-code\">\n<pre><code>HISTSIZE=100<\/code><\/pre>\n<\/div>\n<p>This will show the last 100 or even last 1000 commands. \u00a0This is probably the easiest way to see the commands, but it is also possible to see the last few commands using the fc command (ie fc -l).<\/p>\n<p>Starting a command with a space prevents it from showing up in the list of the previous commands, but it is possible to extend the filtering to other commands. \u00a0There is another variable HISTIGNORE which can be set to include additional commands. \u00a0The variable is just a colon delimited list of commands.<\/p>\n<div class=\"sbody-code\">\n<pre>HISTIGNORE='ssh:fortune:compress'<\/pre>\n<\/div>\n<p>This example will suppress any commands that begin with ssh, fortune or compress. \u00a0This in itself may be enough, to keep those really embarrassing commands that contain passwords out of the history file. \u00a0It is possible to not just supress these commands but any line that contains one of those commands.<\/p>\n<div class=\"sbody-code\">\n<pre>HISTIGNORE='*ssh*:fortune:compress'<\/pre>\n<\/div>\n<p>Simply add the asterisk and then ssh will no longer be added to the command history. This is a shell pattern not a regular expression. \u00a0Only shell patterns will be used, not regular expressions. \u00a0I don&#8217;t have any specific filters that I need, simply adding an asterisk to my command.<\/p>\n<div class=\"sbody-code\">\n<pre>HISTIGNORE=\"[0-9]*\"<\/pre>\n<\/div>\n<p>This filter works but it doesn&#8217;t do much that is meaningful.<\/p>\n<p>It is possible to remove any of those embarassing\u00a0commands by deleting them. \u00a0Simply get a list of the commands and then delete the one based on the index number.<\/p>\n<div class=\"sbody-code\">\n<pre>&gt; history\r\n 1031 ls\r\n 1032 history\r\n 1033 pwd\r\n 1034 cwd\r\n 1035 cd\r\n 1036 history\r\n&gt;\r\n&gt; history -d 1034\r\n&gt; history\r\n 1031 ls\r\n 1032 history\r\n 1033 pwd\r\n 1034 cd\r\n 1035 history\r\n&gt;<\/pre>\n<\/div>\n<p>Actually it is a feature, not a bug.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not a bug it&#8217;s a feature It has been said so many times it goes beyond\u00a0cliche. \u00a0It is really easy to try and explain away odd behaviors as some sort of esoteric feature It took a while before I &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/05\/26\/bash-its-not-a-bug-its-a-feature\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":15,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":969,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions\/969"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}