linuxbashshell

How would I use strings inside a for loop?


I received an assignment that hasn't been completely covered in the learning material (even the person assigned to help students is having trouble helping me) since this is beyond basic bash scripting. I'm not expecting anybody to do my assignment but if I can get a clue or an idea it'll be very helpful!

My assignment:

Code a script in bash linux that will use user's input of number of rows and number of columns, and print 'hello' strong according to the user's input, like so:

For example:
User's input of number of columns:2
User's input of number of rows: 3

hello hello
hello hello
hello hello

I thought in this direction but I can't figure it out and will appreciate any help

echo -e 'Please enter number of rows: \n'
read rows
echo -e 'Please enter number of columns: \n'
read columns

string='hello'
for i in $columns
do
    echo $string
    string+=$string
done

This is as far as I got with the first loop as what I've done here doesn't work.


Solution

  • With two loops:

    #!/bin/bash
    
    string='hello'
    read -p "x:" x
    read -p "y:" y
    
    for ((j=0; j<$y; j++)); do
      for ((i=0; i<$x; i++)); do
        echo -n "$space$string"
        space=" "
      done
      space=""
      echo
    done
    

    See: man bash