1

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?

1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.