Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash loop string and add string to it

Hello I want my script to output the following:

string1 string2
string1 string2 string2
string1 string2 string2 string2
string1 string2 string2 string2 string2

Etc. for about 20 times or so.

I've tried the following:

#! /bin/bash
string1=hello
string2=world

#for {i=1;i=<10;i=i+1};
#echo $string1 + $string2
#!/bin/bash
for ((number=1;number < 10;number++))
do
echo $string1 $string3
string2 *10
done;
exit 0

Now I can't find anything on the web about just looping and adding strings.. Thanks for any help! Greets

like image 839
BillRay Avatar asked Feb 16 '26 21:02

BillRay


1 Answers

Your bash syntax is not correct.

#!/bin/bash

string1=hello
string2=world
output=$string1" "$string2

for i in {1..10} ; do
  echo $output
  output=$output" "$string2
done

Edit: or, taking into account the comments below for the sake of beauty of code:

#!/bin/bash

string1="hello"
string2="world"
output="$string1 $string2"

for ((i = 1; i <= 10; i++)) ; do
  echo "$output"
  output+=" $string2"
done
like image 190
Pierre François Avatar answered Feb 18 '26 23:02

Pierre François



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!