I have following function:
s() { c=$1; while((c--)) ; do printf 'x' ; done }; s 2
So x is printed two times.
Is it possible to simplify this code ?
At first I’d like to skip assignation part c=$1; and do:
while((1--))
Do you have any ideas ?
Try this:
Here the
forloop takes by default the arguments passed to the function and assigns them to i, so you don’t needc=$1anymore.Ok, I haven’t answered your problem directly.
To modify parameters, you must use
set -- word, for example:Your function will be :
But you will lose a little performance and clarity in the script :
While with
s() { c=$1; while (( c-- )); do echo 'x'; done }:I have tried this one :
But the main problem is the creation of a child process for the
evalstatement. The performance is a little faster than above, but still a little difficult to read:Hope this helps.