shell - Unix-Echo -n is not working -
for(( i=0;i<=5;i++)) ((j=1;j<=i;j++)) 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 output be:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
according the manual, -n option echo not portable. on solaris system -n supported bsd emulation of echo in /usr/ucb/echo, not default echo. result:
ksh88's ,ksh'sechonot have-noption.csh's echo ,/usr/ucb/echo, on other hand, have-noption, not understand back-slashed escape characters.sh,ksh88determine whether/usr/ucb/echofound first in path and, if so, adapt behavior of echo builtin match/usr/ucb/echo.
to fix problem, have several options:
switch
printf %s "$j"portably print string without newline. (i recommend doing this.)switch
\cescapes, i.e. replaceecho -n "$j"echo "$j\c". (not recommended if script needs remain portable bsd systems.)download well-tested free shell such
bashimplementsecho -n, , use run shell scripts care about.prepend
/usr/ucbpath. causeechoswitch bsd-compliant behavior, introduce other bsd commands, potentially breaking unrelated parts of script. (not recommended.)
Comments
Post a Comment