I’m trying to write a shell script to print a pyramid of asterisks like this:
JavaScript
x
*
***
*****
*******
*********
***********
*************
***************
*****************
Below is my attempt so far. When I run it, I get some errors that say arithmetic expression required. What am I doing wrong?
JavaScript
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:
JavaScript
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
.)