mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Exclude A Command From The ZSH History File as a Unix TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
37
unix/exclude-a-command-from-the-zsh-history-file.md
Normal file
37
unix/exclude-a-command-from-the-zsh-history-file.md
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user