Skip to content
Advertisement

How to create a script in Linux that makes a pyramid of asterisks with the pattern 1, 3, 5, 7, 9, 11, 13, 15, 17 centered

I’m trying to write a shell script to print a pyramid of asterisks like this:

        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************

Below is my attempt so far. When I run it, I get some errors that say arithmetic expression required. What am I doing wrong?

for (( i = 1; i <= n, i++ )); do
    for (( k = i; k <= n, k++ )); do
        echo -ne " "
    done

    for (( j = 1; j <= 2 * i - 1, j++ )); do
        echo -ne "*"
    done

    echo
done

Advertisement

Answer

The syntax of an arithmetic for-loop uses two semicolons, not a semicolon and a comma:

for (( i = 1; i <= n; i++ )); do

(The individual components may contain commas — for example, i++, j++ is an expression that increments both i and j — but that has no specific relevance to the for.)

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