From 84f4aa2edb58cb6f00f3745a4aab28d5bddbccab Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 4 Jun 2022 16:50:27 -0500 Subject: [PATCH] Add Exclude A Command From The ZSH History File as a Unix TIL --- README.md | 3 +- ...ude-a-command-from-the-zsh-history-file.md | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 unix/exclude-a-command-from-the-zsh-history-file.md diff --git a/README.md b/README.md index c93f06f..bc2fee4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1216 TILs and counting..._ +_1217 TILs and counting..._ --- @@ -1157,6 +1157,7 @@ _1216 TILs and counting..._ - [Do A Dry Run Of An rsync](unix/do-a-dry-run-of-an-rsync.md) - [Do Not Overwrite Existing Files](unix/do-not-overwrite-existing-files.md) - [Enable Multi-Select Of Results With fzf](unix/enable-multi-select-of-results-with-fzf.md) +- [Exclude A Command From The ZSH History File](unix/exclude-a-command-from-the-zsh-history-file.md) - [Exclude A Directory With Find](unix/exclude-a-directory-with-find.md) - [Exclude Certain Files From An rsync Run](unix/exclude-certain-files-from-an-rsync-run.md) - [Figure Out The Week Of The Year From The Terminal](unix/figure-out-the-week-of-the-year-from-the-terminal.md) diff --git a/unix/exclude-a-command-from-the-zsh-history-file.md b/unix/exclude-a-command-from-the-zsh-history-file.md new file mode 100644 index 0000000..429bbfa --- /dev/null +++ b/unix/exclude-a-command-from-the-zsh-history-file.md @@ -0,0 +1,37 @@ +# Exclude A Command From The ZSH History File + +The `zsh` shell can be configured to record the commands you run from the +terminal in a history file. This is great for recalling and retrieving past +commands that you want to run again. + +What about commands that I don't want written to a file on my machine? For +instance, if I'm running a command that includes a password, secret key, or +some other sensitive value, I don't want that saved in plaintext on my machine. + +`zsh` has an affordance for this with the `hist_ignore_space` option. With that +option enabled, any command preceded by a space (`' '`) will be excluded from +the history file. + +First, turn it on. + +```zsh +$ setopt hist_ignore_space +``` + +Now, try a couple commands and see what shows up in the file. + +```zsh +$ echo 'this command will be saved in history' +this command will be saved in history + +$ echo 'this will be kept secret' +this will be kept secret + +$ tail ~/.zsh_history +: 1654378676:0;echo 'this command will be saved in history' +: 1654378690:0;tail ~/.zsh_history +``` + +Notice how the second command with the prefixed space is excluded. + +[source](https://unix.stackexchange.com/a/6104/5916)