I'd like to store a value in an environment variable, but I don't want to do it like this VAR_NAME=secretvalue
, because then it will be stored in the shell's history file. Is there a way to prompt me for the value, just like passwd
does?
Asked
Active
Viewed 1,223 times
1

Gilles 'SO- stop being evil'
- 829,060

aardbol
- 663
-
1Seems related: Is there any way to keep a command from being added to your history? – Freddy May 03 '20 at 18:56
1 Answers
1
If you have the password in the clipboard, use xsel
or xclip
or pbpaste
. See Any function copying from clipboard to a variable in Bash?
Otherwise, assuming that the secret value doesn't contain any newlines (or any control characters or characters that you can't type):
printf 'Password: '
IFS= read -r VAR_NAME
Many shells let you pass the prompt directly to read
:
IFS= read -p 'Password: ' -r VAR_NAME
If you don't want the secret to be visible on your terminal, see Reading passwords without showing on screen in Bash Scripts. Note that even without this, the password won't be saved anywhere once you clear your terminal.

Gilles 'SO- stop being evil'
- 829,060