Skip to content
Advertisement

How to echo group of arguments passed to a script, instead of each individual argument

I have this very basic script:

JavaScript

That when running it:

JavaScript

The output will be an echo of each argument like this:

JavaScript

How can I modify it to echo the arguments in a group of 4 or less (the last group), like this:

JavaScript

Advertisement

Answer

You can use printf:

JavaScript
JavaScript

rematk: At the end of the last line you’ll get a space character for each missing element


Or use a bash substring expansion to get each array slice:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement