neildeadman
neildeadman

Reputation: 3969

echo password to screen as *'s from variable with bash

I have a script that accepts a username and password or uses pre-set variables. I want to echo this info to the user so they know it is set, but don't want to display the actual password.

How can I print the correct number of *'s instead of the password?

Upvotes: 1

Views: 788

Answers (5)

f4m8
f4m8

Reputation: 410

If the visible character must not be '*', then a simple in shell pattern substitution will do the job

echo ${password//?/+}

An asterisk instead of the plus sign will lead to prblem with shell expansions. Escaping the asterisk with backslashes wont ginbt the proper result.

The advantage if the in shell pattern substitution is that there is no external program to pipe through.

Upvotes: -1

jaypal singh
jaypal singh

Reputation: 77145

AWK Solution 1:

[jaypal~/Temp]$ echo "password" | awk '{a=length($0); for (i=1;i<=a;i++) printf "*"}'
********

AWK Solution 2:

[jaypal~/Temp]$ echo "password" | awk '{gsub(/./,"*",$0); print}'
********

Upvotes: 1

Rahul
Rahul

Reputation: 77896

like, if password stored in variable pawd then

for((i=1;i<=(${#pawd});i++));do printf "%s" "*";done;printf "\n"

Upvotes: 0

codaddict
codaddict

Reputation: 455312

To print your password as string of * you can do:

echo $passwd | sed -e 's/./*/g'

Upvotes: 1

Kiran
Kiran

Reputation: 8538

Use read command with a -s option.

Upvotes: 1

Related Questions