Prevent pwgen -y generating backticks or quotes? (or otherwise sanitize output)

Viewed 1919

I'm using pwgen in a bash script. For security, we have to use the -y flag to include at least one special character. However, this frequently returns passwords with one or more of ` or " which break the surrounding script.

Can I prevent these characters being generated? If not, what's the cleanest way to remove, replace or otherwise sanitize pwgen's output to exclude these characters?

My current pwgen is;

intPW=$(pwgen -c -n -y -B -1 15)

which means intPW's value could be something like;

iqui`c:ee4so4Ch
hph7eew"ohr9Ee

I need to guarantee that every password has a symbol in it, just not one of the problematic symbols.

Anything I can think of in terms of replacement, from an if loop to a sed, breaks because I'm trying to target a backtick.

2 Answers

If you have pwgen version 2.08, you can use the new option -r to exclude unwanted characters

pwgen -cnyB1 -r \`\" 15

Assuming you don't also need to protect single quote characters, the following should be OK:

intPW=$(pwgen -c -n -y -B -1 15 | tr '`"' '$@')

That will translate ` to $ and " to @. Feel free to pick any other characters you fancy!

Related