0

I can't seems to see why $crypt is empty here

#!/bin/bash
pass=$(pwgen -1 -n 8)
salt=$(pwgen -1 -n 5) 
crypt=$(perl -le 'print crypt($pass, $salt)')
user=deluge_$(pwgen -s 5 1)
echo crypt: $crypt # no output
echo pass: $pass #work
echo salt : $salt #work
echo $(perl -le 'print crypt($pass, $salt)') # no output

perl -le 'print crypt(Ab3choot, Oa3ah)' # works
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
user2650277
  • 6,289
  • 17
  • 63
  • 132
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jul 28 '16 at 17:42

1 Answers1

0

In bash, variables won't be expanded when put inside single quotes, use double quotes instead:

crypt=$(perl -le "print crypt("$pass", "$salt")")

Now the bash variables, $pass and $salt should be expanded expectedly.

Also almost always, it is a good idea to quote the variables to avoid word splitting and filename expansion.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • 2
    You aren't quoting the variables; you're expanding them unquoted with their values concatenated with the adjacent quoted strings. `perl -le 'my $pass=shift; my $salt=shift; print crypt($pass, $salt)' "$pass" "$salt"` would be safer. – chepner Jul 28 '16 at 17:41
  • 1
    Note that passing the `-w` flag to `perl` would have made this problem more clear: it would have warned that the `$pass` and `$salt` variables are only used once. – ruakh Jul 28 '16 at 17:58