Given a text, $txt, how could I left justify it to a given width in Bash?
Example (width = 10):
If $txt=hello, I would like to print:
hello |
If $txt=1234567890, I would like to print:
1234567890|
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
printfcommand, like this:The
%smeans to interpret the argument as string, and the-10tells it to left justify to width 10 (negative numbers mean left justify while positive numbers justify to the right). The\nis required to print a newline, sinceprintfdoesn’t add one implicitly.Note that
man printfbriefly describes this command, but the full format documentation can be found in the C function man page inman 3 printf.