8

From time to time I find that when I copy/paste a command from a web page (or from the Terminal window itself) the pasted command is not recorded in bash history. For example, just now I did this:

cd foo
    git push --set-upstream origin master
cd ../foo2
    git push --set-upstream origin master
cd ../foo3/
    git push --set-upstream origin master
cd ../foo4
    git push --set-upstream origin master

(Note: I typed the cd commands manually - the git push commands were pasted).

However when I type history I see this:

 2008  cd foo
 2009  cd ../foo2
 2010  cd ../foo3/
 2011  cd ../foo4
 2012  history

Bash version:

GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Linux version:

Ubuntu 14.04 LTS

Terminal version:

Gnome Terminal 3.6.2

This is kind-of annoying, because for one thing history does not show exactly what I did. Secondly, I can't just up-arrow to recall the command I just pasted.

Why is it doing this, and how can I stop it?

Nick Gammon
  • 303
  • 1
  • 3
  • 11
  • Are those spaces literally there ? Before the git commands – Runium May 08 '16 at 23:37
  • Yes, I tried to push on its own (`git push`) and got an error message, suggesting that I do `git push --set-upstream origin master`. The message (from git) was indented like that (I may have the exact number of spaces wrong because the history pushed the message out of the terminal window). I copied the entire line, so some leading spaces are there. – Nick Gammon May 08 '16 at 23:41
  • Thanks! When I searched, and also started typing the question, I did not see that particular thread. Thanks for pointing me to it. – Nick Gammon May 08 '16 at 23:43
  • Np. Easy to miss if you do not know the cause :) Gl – Runium May 08 '16 at 23:45
  • 2
    To change this behavior, check the manpage and look for HISTCONTROL. For exmaple, `export HISTCONTROL=""` in .bashrc would do the trick. – Jeff Learman Aug 07 '18 at 00:14

1 Answers1

11

From the bash manpage:

lines which begin with a space character are not saved in the history list.

The commands you pasted have spaces in front of the command and are therefore not saved in history.

forquare
  • 3,396
  • 5
  • 21
  • 32
  • Ah yes, and I can (of course) read the bash manpage, and I even have a bash book. Still, things like that can slip by you when you aren't expecting them. In general, we expect that bash does not particularly care about spaces. For example `cd#foo` and `cd##foo` (where `#` represents a space) are expected to give the same results. Personally I would like bash to default to not doing that, but I am only one, amongst many. :) – Nick Gammon May 09 '16 at 07:56
  • So what's the workaround? Because commands get executed, but not kept in history. – Vincent Alex Apr 22 '22 at 21:36
  • See the comment on my question. `export HISTCONTROL=""` in .bashrc would do the trick. – Nick Gammon Apr 23 '22 at 06:54