Search code examples
bashperlhashsolaris

Password hash not working with single quotes


I'm trying to hash a password using the crypt hash function in Perl. In a Bash script so far I have:

password='Pa$$word'
hashedPassword="$(perl -e "print crypt('$password', 'salt'), \"\n"")"

I then modify/copy /etc/shadow using sed:

sed -e '/^user1:/s_:[^:]*:_:'"$hashedPassword"':_' /etc/shadow > /tmp/shadow

The method works, except when passing a string containing single quotes. How can I handle a password containing ' single quotes? Running Solaris 10 OS.


Solution

  • Your problem is in bash. Trying to set a shell variable containing a single quote by enclosing it in single quotes will not work. Per the man page:

    Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.