I am creating a password generator however I am not get the passwords to append properly.

Here is my script

while [ $i -lt 5 ]
    echo -n '#' >> passwords.txt && openssl rand -hex 4 >> passwords.txt && echo -n '/' >> passwords.txt && echo -n 'X' >> passwords.txt



Expected Output


How can I make it so that it doesn't skip that first line and also so that it runs each command in that order and will write to the passwords txt file as the expected output.


  • Thanks David King, dhag and lese for all these answers, really appreciate it :) – Ryan Hawdon Dec 17 '15 at 14:07

This should do:

for i in {1..5}; do
    printf '#%s/Z\n' "$(openssl rand -hex 4)"
done >passwords.txt

I replaced the multiple calls to echo with a single call to printf. Having the call to openssl wrapped inside a command substitution has the side effect of making the line ending disappear, and that newline character was the cause of the badly-placed line breaks visible in your example.


The problem is that openssl is being 'helpful' and placing a newline at the end of the random string. You can remove that by piping it through tr

openssl rand -hex 4 | tr -d '\n'
  • Thank you so much :) I have been spending a while trying fix this. Thanks. – Ryan Hawdon Dec 17 '15 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.