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?

5
  • Are those spaces literally there ? Before the git commands
    – Runium
    May 8, 2016 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. May 8, 2016 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. May 8, 2016 at 23:43
  • Np. Easy to miss if you do not know the cause :) Gl
    – Runium
    May 8, 2016 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. Aug 7, 2018 at 0:14

1 Answer 1

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.

4
  • 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. :) May 9, 2016 at 7:56
  • So what's the workaround? Because commands get executed, but not kept in history. Apr 22, 2022 at 21:36
  • See the comment on my question. export HISTCONTROL="" in .bashrc would do the trick. Apr 23, 2022 at 6:54
  • Also see <unix.stackexchange.com/questions/115917> Apr 23, 2022 at 6:56

Not the answer you're looking for? Browse other questions tagged .