Reputation: 1
for(( i=0;i<=5;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$j"
done
echo " "
done
Outputs:
-n 1
-n 1
-n 2
-n 1
-n 2
-n 3
-n 1
-n 2
-n 3
-n 4
-n 1
-n 2
-n 3
-n 4
-n 5
My OS: SunOS sun4v sparc sun4v
I want the output to be:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Upvotes: 0
Views: 1721
Reputation: 1850
If -n
option is not working for you, you can try using -e
option which enables the use of backslash characters and then you can use \c
in your echo
statement.
Here is the code sample:
for(( i=0;i<=5;i++))
do
for ((j=1;j<=i;j++))
do
echo -e "$j \c"
done
echo " "
done
Upvotes: 0
Reputation: 155216
According to the manual, the -n
option to echo
is not portable. On a Solaris system -n
is supported by the BSD emulation of echo
in /usr/ucb/echo
, but not by the default echo
. As a result:
ksh88
's andksh
'secho
does not have an-n
option.csh
's echo and/usr/ucb/echo
, on the other hand, have an-n
option, but do not understand the back-slashed escape characters.sh
andksh88
determine whether/usr/ucb/echo
is found first in the PATH and, if so, they adapt the behavior of the echo builtin to match/usr/ucb/echo
.
To fix the problem, you have several options:
Switch to printf %s "$j"
to portably print a string without newline. (I would recommend doing this.)
Switch to \c
escapes, i.e. replace echo -n "$j"
with echo "$j\c"
. (Not recommended if the script needs to remain portable to BSD systems.)
Download a well-tested free shell such as bash
that implements echo -n
, and use it to run the shell scripts you care about.
Prepend /usr/ucb
to the PATH
. This will cause echo
to switch to BSD-compliant behavior, but will also introduce other BSD commands, potentially breaking unrelated parts of the script. (Not recommended.)
Upvotes: 7