2

"-s" switch is not working bash shell script. It throws error without prompting for the password.

Using: Ubuntu 16.04.6 LTS

Am i missing anything?

#!/bin/bash

echo "Enter password"
read -s password
echo "password:" $password

Enter password test.sh: 4: read: Illegal option -s password:

2
  • What's the output of type read? Commented Jul 2, 2019 at 10:14
  • output: read is a shell builtin Commented Jul 2, 2019 at 12:20

1 Answer 1

2

You are running the script with sh, not bash which provides the functionality in question. Either invoke the script directly with bash:

bash test.sh

Or make the file executable so the interpreter will read your shebang:

chmod +x test.sh    # only required once
./test.sh

The .sh extension is unnecessary unless you are running actual POSIX shell scripts in certain special directories.

Sign up to request clarification or add additional context in comments.

2 Comments

bash test.sh works. Thanks. can you pls share the details or some article on difference?
sh is POSIX Shell. It's very old. bash is the Bourne Again SHell; a replacement that has evolved into a kind of hybrid command/scripting language. Bash has so many features that it's not fair to compare the two, but there are very old systems which don't have it. We also use sh to run boot sequences without loading all the resources that bash requires.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.