the same tr translate failed on some of machine

0

I use the following code and failed.

$ echo $TERM
xtermc
$ v=/bin:/usr/bin:/usr/local/bin ; echo $v | tr ':' '\n'
/binn/usr/binn/usr/local/bin

It seems skip my '/'. I don't know how to solve this. My shell is zsh. I've changed to bash, but still failed.

But, on another machine, it works. and the result is

/bin
/usr/bin
/usr/local/bin

I found there are three tr

/usr/local/bin/tr (GNU coreutils) 6.4
/usr/ucb/tr unkown version size 9916 bytes
/bin/tr size 19400 Usage: /bin/tr [ -cds ] [ String1 [ String2 ] ]

Daniel YC Lin

Posted 2014-03-13T02:50:13.823

Reputation: 795

Are both machines running Solaris? Could you post the output of which tr and tr --version on both machines? – Dennis – 2014-03-13T03:09:58.857

Answers

1

The problem is caused by different tr and different PATH setup.

works: /usr/local/bin/tr (GNU coreutils) 6.4
failed: /usr/ucb/tr unkown version size 9916 bytes
works: /bin/tr size 19400 Usage: /bin/tr [ -cds ] [ String1 [ String2 ] ]

Daniel YC Lin

Posted 2014-03-13T02:50:13.823

Reputation: 795

0

Since you are using zsh, you can use this approach and won't rely on any external command anymore:

$ v=/bin:/usr/bin:/usr/local/bin; print -l ${(s.:.)v}
/bin
/usr/bin
/usr/local/bin

How it works:

  • the parameter expansion flag (s.:.) splits the parameter $v at colons (see man zshexpn)
  • print -l prints every following argument on a single line

If you rely an the newline character, use

$ print ${v//:/\\n}

where //from/to does a global (two beginning slashes) search & replace.

mpy

Posted 2014-03-13T02:50:13.823

Reputation: 20 866