Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove newline from output?

Tags:

shell

Hashing passwords in shell (sha512) breaks the line. How to get a result in one line?

The script for hashing:

password="abc123"
hashPassw="$(/bin/echo -n "${password}" | openssl dgst -binary -sha512 | openssl enc -base64)"
echo "${hashPassw}"

Output is (why breaks the line?):

xwtd2ev7b1HQnUEytxcMnSB1CnhS8AaA9lZY8DEOgQBW5nY8NMmgCw6UAHb1RJXB
afwjAszrMSA5JxxDRpUH3A==

Should be one line:

xwtd2ev7b1HQnUEytxcMnSB1CnhS8AaA9lZY8DEOgQBW5nY8NMmgCw6UAHb1RJXBafwjAszrMSA5JxxDRpUH3A==
like image 341
kpietru Avatar asked Mar 04 '16 15:03

kpietru


1 Answers

From the OpenSSL Wiki for enc.

To suppress this you can use in addition to -base64 the -A flag. This will produce a file with no line breaks at all.

So adding the additional -A flag will do the trick.

password="abc123"
hashPassw="$(/bin/echo -n "${password}" | openssl dgst -binary -sha512 | openssl enc -A -base64)"
echo "${hashPassw}"

Which outputs

xwtd2ev7b1HQnUEytxcMnSB1CnhS8AaA9lZY8DEOgQBW5nY8NMmgCw6UAHb1RJXBafwjAszrMSA5JxxDRpUH3A==
like image 120
James Avatar answered Oct 06 '22 17:10

James