How to safely enter passwords in a terminal

| · @kyrylosilin · bluesky:@kyrylo.org

Sometimes command line programs allow you to pass passwords to switches. For example, MySQL: mysql -u user -p password. However, if your shell records history, you’re risking to store your password in a plain text file. There’s a way to avoid this. Depending on your shell add the following line.

Zsh

# ~/.zshrc
setopt HIST_IGNORE_SPACE

Bash

# ~/.bashrc
HISTCONTROL=ignorespace

Result

Now you can prepend a space character before your command. Such a command won’t be stored in your history.

% whoami
linuxhacker
%  mysql -u user -p password
% # do your business... exit MySQL
% Bye.

Press Arrow_Up. The MySQL command isn’t in your history.

% ↑
% whoami

← How to say “I love you” in ... · Home · Blocks in Ruby are always t... →