Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Long time i am searching for this, i would like to know is it possible to pass passwords in a shell script? Many of the answers returned with no. Recently i read an article stating that how to pass passwords in a shell script. I have tried that, but it doesn't seem to work. This is the link. Can anyone checkitout and revert back? Also pls say me is there a way to pass passwords in a shell script? If no pls say me how linux gets the input for the password?

share|improve this question
up vote 9 down vote accepted

By "entering passwords", you likely mean entering data without being visible for the user.

(suggested by geirha) When using bash, you can use the -s option to prevent typed characters from being displayed:

read -p "Password please: " -s pass

Alternatively, change the behavior of the terminal to hide typed characters with stty -echo (disable echo). After reading the password with the shell built-in read into a variable (in the below example, $pass), turn it back on with stty echo. Because the new line from Enter is hidden to, you've to print a newline to get future output on a new line.

stty -echo
read -p "Password please: " pass
stty echo
printf '\n'

read and printf are shell built-ins. stty is provided by the coreutils package which is installed by default. That means that this snippet is very portable.

Note: the -p option is not standard, but from bash. If you need to display a prompt in other shells, use:

printf "Password please: "
stty -echo
read pass
stty echo
printf '\n'


share|improve this answer
I just want to give the root password of the remote system in the script. Is it possible in your case? – karthick87 Jun 3 '11 at 19:58
@karthick87: no, this script just captures the data the user types in interactively. If you want to put root passwords in scripts, why don't you consider keybased logins? – Lekensteyn Jun 3 '11 at 20:23
Bash's read also has an option to disable echoing while reading in the line. See help '\read'. – geirha Jun 5 '11 at 12:08
In zsh it is achievable through read -s 'pw?Prompt text', where pw is a variable where password will be placed and -s is a flag to hide input. – baldrs Nov 20 '13 at 13:34

To which program do you want to pass a password?

The script on the link works for me. Note that is not a shell script but an expect script (needs the package expect to be installed`). Using expect is a common way to automate text based interactive programs.

Non-interactive ssh logins are often done using key-based authentication with an empty passphrase.

Some other programs (like sudo) have options to read a password from stdin.

Providing a password as a command line option is often a security problem as on most systems any user can see any other users processes including there command line arguments using simple tools like ps.

share|improve this answer
I just want to pass a password to ssh program. – karthick87 Jun 3 '11 at 19:59
So maybe key authentication works for you: Use ssh-keygen to create a authentication key (just press Enter when asked for a pass phrase) and copy it to the remote user using ssh-copy-id.If key authentication is enabled for sshd (it is by default) ssh will then not ask you for a password. – Florian Diesch Jun 3 '11 at 21:13

A script should never really handle passwords. Have whichever application needs the password ask for it itself, or if that's not possible, find a better means of authenticating with the application. Please read

share|improve this answer

There's a way to store passwords in a bash script but you have to encrypt the script so no one can actually read it, or run any type of debugger on it to see exactly what it is doing. To encrypt a bash/shell script and have it actually be executable, try copying and pasting it here

On the above page, all you have to do is submit your script (you can submit a sample script first for your peace of mind). A zip file will be generated for you. Right click on the download link and copy the URL you're provided. Then, go to your UNIX box and perform the following steps.


   wget link-to-the-zip-file
   unzip the-newly-downloaded-zip-file
   cd /tmp/KingLazySHIELD
   ./  /var/tmp/KINGLAZY/SHIELDX-(your-script-name) /home/(your-username)  -force 

What the above install command will do for you is:

  1. It'll install the encrypted script in the directory /var/tmp/KINGLAZY/SHIELDX-(your-script-name).

  2. It'll place a link to this encrypted script in whichever directory you specify in replacement of /home/(your-username) - that way, it allows you to easily access the script without having to type the absolute path.

  3. Ensures NO ONE can modify the script - Any attempts to modify the encrypted script will render it inoperable...until those attempts are stopped or removed.

  4. Ensures absolutely NO ONE can make copies of it. No one can copy your script to a secluded location and try to screw around with it to see how it works. All copies of the script must be links to the original location which you specified during install.


I don't believe this works for interactive scripts that prompts the user for a response. The values should be hardcoded into the script. The encryption ensures no one can actually see those values so you need not worry about that.

share|improve this answer

you can simply do something like this.

# echo -e "password_here\npassword_here" | su user


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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