Hacker News new | ask | show | jobs
by hamosch 1512 days ago
Learnt a neat trick from an old sysadmin colleague.

If you’ve written a command but realize you don’t want to run it right now but want to save it in your history you can just put a `#` in front of it (ctrl-a #) making it a comment and allowing you to save it in your history without running it.

When you’re ready to run it you find it and remove the preceding `#`

6 comments

The opposite is adding space before the command. The command will run but it will not be saved in history.

EDIT: This apparently needs to be configured - setting HISTCONTROL=ignorespace

I had been in the habit of symlinking ~/.bash_history to /dev/null to avoid AFS/NFS writes on every local command execution. When I moved over to the financial industry, it didn't occur to me that such a symlink might look like an attempt to evade monitoring. A year or two in, I realized it didn't look good, but it had clearly been made my first week on the job, so I just left it in place for over 10 years rather than risk looking like I was again monkeying with my history.

I hope and presume they had much better monitoring than scanning bash history, but I'm not bet-my-career confident of that.

> I hope and presume they had much better monitoring than scanning bash history, but I'm not bet-my-career confident of that.

bash has an "audit" function which is normally compiled out.

https://git.savannah.gnu.org/cgit/bash.git/tree/configure#n1...

When enabled it logs to syslog.

Enterprises that requires logging of user actions will very likely not being doing it at the shell level, either through compiled in options, or shell history.

Instead, the Kernel has built in functionality called Auditd[0], which is capable of logging any and all executions, file or socket accesses, and much more. Along with included tooling for quickly finding and alerting on events[3].

Further, if terminal logging or playback is really required (usually not), it's generally done through pam with tlog[1]. Red Hat 8 and above come with built-in tlog support[2].

[0] https://access.redhat.com/documentation/en-us/red_hat_enterp...

[1] https://github.com/Scribery/tlog/blob/main/README.md

[2] https://access.redhat.com/documentation/en-us/red_hat_enterp...

[3] https://wiki.archlinux.org/title/Audit_framework

It's simpler to use a tmpfs for this purpose. $XDG_RUNTIME_DIR is already available, on modern Linux versions.
systemd-tmpfiles can be configured to delete a path upon ‘systemd-tmpfiles —-user clean’
Thanks to your comment, I learnt about ignoredups as well
And `ignoreboth` to combine the two.
Lol that makes sense, never thought of commenting out the command but I guess I do something similar. If i realize i dont want a command yet I enter it with a trailing `\`, then `CTRL+C` to get back to an empty prompt.
This is useful when saving command lines to files (scripts) using the POSIX-required fc builtin. Command line histories are relatively cumbersome to save with Ash, Bash saves them but truncates them to 500 entries, whereas scripts can easily be saved indefinitely. Amongst Bash and other feature-heavy shell users, there are Rube Goldberg-like workarounds for command line history saving. OTOH, all shells aiming for POSIX compliance, including the fastest, lightest weight ones I prefer, will implement fc. It's already there; I make use of it.

I will type fc, save to a file (script) and then delete all lines before exiting the default EDITOR, e.g., %d in vi. This prevents the commands from being re-executed when I exit vi.

Also I sometimes use # combined with a semicolon to disable portions of command lines, e.g., early commands ;# late commands. I might cut and paste from one entry in the history into another one. Or I might fc -l 1 > file and edit the file down the the entries that form the starting point for a new script. By far, the shell is the most useful REPL for me.

There is no shortage of comments online praising the utility of the REPL concept but the only comment I have ever seen about fc was from a shell implementor/maintainer; it was negative. I use fc all the time. It has become essential for me to use the shell effectively as a REPL.

> Bash saves them but truncates them to 500 entries

That behavior can be modified with the HISTSIZE environment variable.

> The maximum number of commands to remember on the history list. If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.

https://www.gnu.org/software/bash/manual/html_node/Bash-Vari...

Bash is too complicated. Too many options. I am not smart enough to use it. I prefer the Almquist shell. No shellshock.

   echo "export PROMPT_COMMAND='history -a;history -r;export HISTFILE=-1;export HISTFILESIZE=-1;shopt -s histappend'" > .bashrc

   cp .bashrc /etc/profile
When I peruse bash-5.1/CHANGES it makes me uncomfortable. It is far too long, for one.
You can achieve the same thing by typing a command, then hitting Escape followed by '#'.

It will prefix your current commandline with a '#' and "run" it.

I wonder if it isn't possible to get it to save your command to history when you do Ctrl-C.

I tried a naive way by trapping sigint but couldn't get it to work.

alt-# is quite enough.