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's echo 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:

  1. switch printf %s "$j" portably print string without newline. (i recommend doing this.)

  2. switch \c escapes, i.e. replace echo -n "$j" echo "$j\c". (not recommended if script needs remain portable bsd systems.)

  3. download well-tested free shell such bash implements echo -n, , use run shell scripts care about.

  4. prepend /usr/ucb path. cause echo switch bsd-compliant behavior, introduce other bsd commands, potentially breaking unrelated parts of script. (not recommended.)


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -