Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Fun fact: if you don't want to put a command in history, prefix it with a space. This works in many shells.


sort by: page size:

And if you know don't want a command in your history, prefix it with a space.

A space before the command stops it from going in your history, though.

If you don't want one command to end up in your history, type a space before the command.

Just pop a space at the beginning and it won't go into shell history.

... or prefix your commands with whitespace to skip ~/.bash_history altogether.

I like that on the remote server he prefixes commands with a space so they won't get saved in the history file.

This feature seems to exist also in bash. I discovered it accidentally while wondering why my history did not include my last commands (copy/pasted with a leading space).

Tip: you can prepend a space at the beginning of the command and it won't be saved in the history (though I agree that letting the command prompt for a password is better)

Only tangentially related, but if you prefix the command with a space, it doesn't get recorded to history.

    $ cd
    
    $ echo "one"
    one
    
    $  echo "two"
    two
    
    $ echo "three"
    three
    
    $ history | tail -3
    10089  cd
    10090  echo "one"
    10091  echo "three"

When you remember to do it, that is.

Pro-tip: if you wanna hide stuff from your shell history, enter a space/blank before the actual command.

Not that it probably would have helped in this situation, but maybe useful for others.

Usually if I have to enter secrets in my shell, I do something like this in order to save the useful stuff to history but hiding the secrets:

- ` export PASSWORD=hunter2` (notice the space before "export")

- `service-cli login username $PASSWORD` (no space before, gets saved to shell history)


I used to do this, but with fish shell unless I have very similar commands, I just type part of the whole line to something I (personally) associate it with and that’s it, that’s the command.

I press a space before commands with sensitive information so they don’t stay in my history or throwaway commands that I’m 99% sure I won’t use again.

Complicated ones I create a function for it, but I no longer carry aliases on my shell configuration, and I prefer the legibility of my commands/history.


Heh. This is funny timing, as .bash_history made me feel like an idiot just yesterday morning.

I was sitting here between queries and I thought 'y'know, all I ever do with history is grep for things. I wonder what else I can do?' When I want to learn about something quick, I usually start with the built-in help; so I did

history --help

The built-in help for history, unfortunately, is woefully inadequate; this was the output:

history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]

I said to myself: "Hmm. Wonder what those do?" -- and in a moment of bash stupidity, I thought: "well, let's try it!"

history -c

There's no output from that command, but it didn't take long to figure out what it does. It deletes your entire bash history, clearing the history in RAM and emptying .bash_history.

Since I use history all the time, I panicked. Holy crap! What have I done? I spent a few minutes internalizing the obvious lesson that everybody knows - never run commands without knowing them first. Thankfully, though, I had another bash open at the time. And after I'd beaten myself up over it, I thought for a moment, jumped over to the other shell, and did:

cd && history | cut -c 8- >> .bash_history

... because (after all) the evil, evil history -c command doesn't kill the history from RAM in other shells.

Lucky me.


Thank you!! Why on earth isn't that the default. It always seemed weird that with multiple bash windows open, the commands from most of them weren't added to the history.

Just put a space before you execute the command, then it doesn't get saved in history. Alternatively, delete it from your history file as the other commenter suggests.

You can view the history (with the line numbers) by typing 'history'. If you have the following set, you can avoid any command prefixed with a space ever getting in this list. HISTCONTROL=ignorespace

I have trained myself, over many years, to hit space automatically before I type rm/reboot/shutdown etc. It's occasionally inconvenient if I need to rerun something but does mean I can't fat-finger anything destructive!


I've just tried and I can't reproduce. It might be because you're copying a leading space: if a command is prefixed by white space it doesn't get saved in the history.

With bash's HISTIGNORE, I can consciously prefix my command with a space to prevent it being added to history.

ls I usually don't care about, but there are directories I regularly cd to, so it would be nice to have those in history.

I can think of a neat heuristic, which is that I often cd to an absolute or home directory, so if the path starts with / or ~ I'll possibly want to cd there again in the future. Changing to a relative path on the other hand, I tend to do more rarely and while doing more ephemeral work.


Wow, I did not know that!

I would have referred to this using !$, which is one of the Bourne shell history substitutions. Like

  $ mkdir awesome
  $ cd !$
(The "!" has the connotation "history" in the shell, while the "$" has the connotation "last", as in regular expressions.)

An advantage of the way you mentioned is that you could actually see the history item displayed on the command line before running the command!


Good tool, but my history is so noisy I had to find another way. Lately I started to add a new habit in my shell actions.

When a command has some cognitive requirements I create a script with some ${1:-default} values and I store them all in $PATH enabled local/bin

All my scripts start with __ and then some capital letters indicating the topic.

I can copy them to new machines, which is something I would not do with my history.

next

Legal | privacy