How Do You Assign a Password to a Variable In Linux Without Making It Visible?

Goal:  You want to issue a command (e.g., an export command) that has a password in it.  But you do not want anyone looking over your shoulder to see it.  You do not want the password to be visible in the history of the shell.
Solution:  Type this 'read -s foobar'
Then press enter.  The cursor will go to the next line.  Type in the password.  The characters will not echo to the screen.  The cursor will not advance.  Press enter.  Now you can use the variable like this: $foobar
For example, you can use export special_setting=connectionString@format:jdoe:$foobar
You will now have exported a setting that will be interpret "$foobar" as the password you entered.  The content of the variable will never be displayed.  It is important to remember that the variable name in the first "read -s foobar" statement has no dollar sign "$".  While the variable's reference, for evaluation, uses a dollar sign "$".  This is a convention in bash scripts as well as interactive bash commands.

Leave a comment

Your email address will not be published. Required fields are marked *