Contents
How to repeat a char using printf or puts?
You’ll have to either write a loop using printf or puts, or write a function that copies the string count times into a new string. printf doesn’t do that — and printf is overkill for printing a single character.
How to add a new line using printf?
Thanks in advance for the help! printf ‘ %s ‘ ‘I want this on a new line!’ That allows you to separate the formatting from the actual text. You can use multiple placeholders and multiple arguments. Thanks for contributing an answer to Stack Overflow!
Why do I need a printf at the end?
If I give a printf at the end I want to see all the lines. and of course you need your buffer to be big enough. Here is a version with some resistance to errors. It is useful if you do not care when errors happen so long as you can continue along your merry way when they do.
What does the sprintf function return from vsprintf?
The function returns the value from vsprintf, which in most implementations is the number of bytes written to the destination. There are a few holes in this implementation, but it should give you some ideas.
How many arguments are in the printf command?
When you use {1..5000}, the shell expands it and it becomes: i.e., the printf command now has 5000 arguments, and for each argument, you will get one H. These don’t have to be sequential or numeric: prints HHHHH — i.e., the number of arguments, 5 in this case.
Is there a way to print any number of characters?
It’s a one-liner that prints any number of repeating characters! The current accepted answer for this question (by ghostdog74) provides a method that executes extremely slowly for even a moderately high number of characters. The top-voted answer (by CaffeineConnoisseur) is better, but is still quite slow.
Which is faster to print a character in Bash?
The top-voted answer (by CaffeineConnoisseur) is better, but is still quite slow. Here is what, in my tests, has executed fastest of all (even faster than the python version): And in fourth place is the one using the bash printf built-in along with tr: