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
'secho
not have-n
option.csh
's echo ,/usr/ucb/echo
, on other hand, have-n
option, not understand back-slashed escape characters.sh
,ksh88
determine whether/usr/ucb/echo
found 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
\c
escapes, i.e. replaceecho -n "$j"
echo "$j\c"
. (not recommended if script needs remain portable bsd systems.)download well-tested free shell such
bash
implementsecho -n
, , use run shell scripts care about.prepend
/usr/ucb
path
. causeecho
switch bsd-compliant behavior, introduce other bsd commands, potentially breaking unrelated parts of script. (not recommended.)
Comments
Post a Comment