2

I am trying to read a password from user, i used -s silent flag but read -s is not working from the script but it works if i do it manually from terminal.

error details

project.sh: 3: read: Illegal option -s
you entered 

code

maddy@ElementalX:~/Desktop$ cat project.sh 
#!/usr/bin/sh

read -s -p "Enter Password: " pswd
echo "you entered $pswd"
maddy@ElementalX:~/Desktop$ 

1 Answer 1

4

The -s option to the built-in utility read is not a standard option, and is unlikely to be implemented in sh. Likewise, the -p option for giving a custom prompt is unlikely to be implemented by a generic sh.

Run your script with bash instead, whose read does support -s for reading from the terminal without echoing the typed-in characters (and also -p). The easiest way to do this is to change the #!-line to point to the bash executable on your system.

In a non-bash shell, you may get a similar effect with

printf 'Enter password: ' >&2

stty -echo
read password
stty echo
2
  • it's working now, but i just want to clear what's wrong with sh only ?? because same thing was working if i do it directly from terminal. Commented May 11, 2019 at 9:46
  • @MadhusudanBabar Your sh is not bash, and its read does not implement -s nor -p. Your shell in the terminal is bash, so it supports read -s. Commented May 11, 2019 at 9:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.