Re: echo -n

2017-02-02 Thread Stephane Chazelas
2017-02-02 22:26:22 +0530, Jyoti B Tenginakai:
[...]
> I have tried using the printf instead of echo. But the issue with printf
> is , the behaviour is not consistent with what echo prints for all the
> inputs i.e.
> In my script I am generically using echo for all the options. If I have to
> use printf instead of it should behave consistently .
> if echo * is passed to bash shell, the o/p shows the \t seperated values
> whereas with printf '%s'  *, it won't display space separated output. Again
> printf '%s ' # behaviour is different from what echo # shows
[...]

See also:

https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo


In bash, you can define:

puts() {
  local IFS=' '
  printf '%s\n' "$*"
}

as a function that outputs its arguments separated by spaces and
terminated with a newline character.

POSIXly:

puts() (
  IFS=' '
  printf '%s\n' "$*"
)

With with some shells like bash, that implies an additional
fork.

Note hat ksh and zsh also have:

print -r -- *

for that.

-- 
Stephane




Re: echo -n

2017-02-02 Thread Chet Ramey
On 2/2/17 11:56 AM, Jyoti B Tenginakai wrote:
> HI All,
> 
> Thanks for your quick response.
> 
> I have tried using the printf instead of echo. But the issue with printf is
> , the behaviour is not consistent with what echo prints for all the inputs
> i.e.
> In my script I am generically using echo for all the options. If I have to
> use printf instead of it should behave consistently .
> if echo * is passed to bash shell, the o/p shows the \t seperated values
> whereas with printf '%s' *, it won't display space separated output. Again
> printf '%s ' # behaviour is different from what echo # shows

echo()
{
builtin printf "%s\n" "$*"
}

You can make this more elaborate if you want.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: echo -n

2017-02-02 Thread John McKown
On Thu, Feb 2, 2017 at 10:56 AM, Jyoti B Tenginakai 
wrote:

> HI All,
>
> Thanks for your quick response.
>
> I have tried using the printf instead of echo. But the issue with printf
> is , the behaviour is not consistent with what echo prints for all the
> inputs i.e.
> In my script I am generically using echo for all the options. If I have to
> use printf instead of it should behave consistently .
> if echo * is passed to bash shell, the o/p shows the \t seperated values
> whereas with printf '%s' *, it won't display space separated output. Again
> printf '%s ' # behaviour is different from what echo # shows
>
​Yes, it is. You can change the output by using the appropriate "format"
(the '%s' in your example). For example:

echo -n *

could be emulated with:

printf '%s ' * #note the space in the format after the %s

The outputs are not _identical_ because the printf output has a trailing
space, which the echo does not.​ If you want tab separators, try:

printf '%s\t' *

​If you want something which 100.% works exactly and identically like
the BASH "echo" builtin, then you are doomed to disappointment. If you want
a replacement for "echo -n", you might try using a function (defined in
~/.bashrc) similar to:

function echo-n() { printf '%s ' "$@" | sed -r 's/ $//'; }​

>
> Thanks & Regards
> --Jyoti
>
> --
There’s no obfuscated Perl contest because it’s pointless.

—Jeff Polk

Maranatha! <><
John McKown


Re: echo -n

2017-02-02 Thread Greg Wooledge
On Thu, Feb 02, 2017 at 10:26:22PM +0530, Jyoti B Tenginakai wrote:
> I have tried using the printf instead of echo. But the issue with printf
> is , the behaviour is not consistent with what echo prints for all the
> inputs i.e.

But what echo prints is by definition inconsistent across platforms and
shells and so on.

Make the code do what you want.  Do not attempt to make it use a broken
command in some consistent way.

> In my script I am generically using echo for all the options.

Why?  What do you want to write to stdout?  Figure out what you want to
do, then do it.

> If I have to
> use printf instead of it should behave consistently .

printf behaves consistently.

> if echo * is passed to bash shell

... what does that mean?  Your user is passing a quoted string 'echo *'
as an argument?  What are you doing, eval'ing user arguments as shell
commands?

Or do you mean that you have the command   echo *   in your script?

What do you WANT it to do?

Do you want it to print the files in $PWD one per line?  Blithely ignoring
the possibility of filenames that contain newlines?

printf '%s\n' *

That will print each filename (except dot files, but your original command
has the same issue) with a newline after it.

If a filename contains a newline, then there will be two newlines
produced for that particular filename: the one inside it, and the one
added by the printf format spec.

> the o/p shows the \t seperated values

The who what?  What the hell are you talking about now?

> whereas with printf '%s'  *, it won't display space separated output.

You want SPACES now?

printf '%s ' *

> Again
> printf '%s ' # behaviour is different from what echo # shows

Are you talking aobut the one trailing space that you'll get from

printf '%s ' *

vs.

echo *

??

Do you actually CARE about ONE SPACE?

Do you have an actual GOAL here?  Or are you just flailing around
randomly until you stumble across some way to bitch about printf?

I really hate it when people have a desired answer and then keep
flailing around at the English language until they come up with a
question that they think will give them their desired answer.

In your case, your desired answer is apparently "to run the command echo *".

Nobody can possibly satisfy your demands, because you don't have a goal.



Re: echo -n

2017-02-02 Thread Jyoti B Tenginakai

HI All,

Thanks for your quick response.

I have tried using the printf instead of echo. But the issue with printf
is , the behaviour is not consistent with what echo prints for all the
inputs i.e.
In my script I am generically using echo for all the options. If I have to
use printf instead of it should behave consistently .
if echo * is passed to bash shell, the o/p shows the \t seperated values
whereas with printf '%s'  *, it won't display space separated output. Again
printf '%s ' # behaviour is different from what echo # shows

Thanks & Regards
--Jyoti



Jyoti Tenginakai
AIX-Security Development Team
IBM India Software Lab
EGD  'D' Block Sixth Floor
Off Indiranagar Koramangala Intermediate Ring Road
Bangaluru - 560071
ph: 4177
extn: 7
Mail:jyoti@in.ibm.com





From:   Pierre Gaston 
To: Sangamesh Mallayya 
Cc: "bug-bash@gnu.org" , Jyoti B Tenginakai

Date:   02/02/2017 08:45 PM
Subject:Re: echo -n





On Thu, Feb 2, 2017 at 11:02 AM, Sangamesh Mallayya <
sangamesh.sw...@in.ibm.com> wrote:
  Hi,

  description:
  in bash echo -n , echo -e , echo -E has a special meaning. But we do not
  have a way in bash shell if we want to print
  -n , -e and -E using echo command. Other shells supports printing of
  -n/-e/-E options using echo command.

  For example

  with ksh
  # echo -n
  -n
  #

  with bash
  # echo -n

  #

  Please let us know if this a bug or do we have any other option to print
  -n ?

  Here is the environment details.

  version: bash 4.3
  Hardware and Operating System P7 AIX
  Compiled with AIX xlc

  Thanks,
  -Sangamesh




Not a bug, echo is not portable and posix recommends using printf e.g.

printf '%s\n' -n


Re: echo -n

2017-02-02 Thread Eduardo Bustamante
El jue., feb. 2, 2017 9:00 AM, Sangamesh Mallayya <
sangamesh.sw...@in.ibm.com> escribió:

> [...]
>
> Please let us know if this a bug or do we have any other option to print
> -n ?
>

Use the printf builtin command. What you encountered is a known limitation
of the echo command, as specified by POSIX.


Re: echo -n

2017-02-02 Thread Pierre Gaston
On Thu, Feb 2, 2017 at 11:02 AM, Sangamesh Mallayya <
sangamesh.sw...@in.ibm.com> wrote:

> Hi,
>
> description:
> in bash echo -n , echo -e , echo -E has a special meaning. But we do not
> have a way in bash shell if we want to print
> -n , -e and -E using echo command. Other shells supports printing of
> -n/-e/-E options using echo command.
>
> For example
>
> with ksh
> # echo -n
> -n
> #
>
> with bash
> # echo -n
>
> #
>
> Please let us know if this a bug or do we have any other option to print
> -n ?
>
> Here is the environment details.
>
> version: bash 4.3
> Hardware and Operating System P7 AIX
> Compiled with AIX xlc
>
> Thanks,
> -Sangamesh
>
>
>
>
Not a bug, echo is not portable and posix recommends using printf e.g.

printf '%s\n' -n


Re: echo -n

2017-02-02 Thread John McKown
On Thu, Feb 2, 2017 at 3:02 AM, Sangamesh Mallayya <
sangamesh.sw...@in.ibm.com> wrote:

> Hi,
>
> description:
> in bash echo -n , echo -e , echo -E has a special meaning. But we do not
> have a way in bash shell if we want to print
> -n , -e and -E using echo command. Other shells supports printing of
> -n/-e/-E options using echo command.
>
> For example
>
> with ksh
> # echo -n
> -n
> #
>
> with bash
> # echo -n
>
> #
>
> Please let us know if this a bug or do we have any other option to print
> -n ?
>

​Well, as another told me "echo is _evil_". You could use "printf" instead:

$ printf '%s\n' '-n'
-n



-- 
There’s no obfuscated Perl contest because it’s pointless.

—Jeff Polk

Maranatha! <><
John McKown


Re: echo -n

2017-02-02 Thread Greg Wooledge
On Thu, Feb 02, 2017 at 02:32:00PM +0530, Sangamesh Mallayya wrote:
> in bash echo -n , echo -e , echo -E has a special meaning. But we do not 
> have a way in bash shell if we want to print 
> -n , -e and -E using echo command. Other shells supports printing of 
> -n/-e/-E options using echo command. 

Use printf instead of echo.

printf '%s\n' "$myvariable"

This will work regardless of the contents of the variable.

echo is deprecated, and should not be used unless you are certain that
it will not be given an argument that could be misinterpreted as an
option.

E.g. this is fine:

echo "You scored $points points."

This is not fine:

echo "$foo"

This was NEVER acceptable and you should not even THINK it:

echo $foo



echo -n

2017-02-02 Thread Sangamesh Mallayya
Hi,

description: 
in bash echo -n , echo -e , echo -E has a special meaning. But we do not 
have a way in bash shell if we want to print 
-n , -e and -E using echo command. Other shells supports printing of 
-n/-e/-E options using echo command. 

For example 

with ksh 
# echo -n
-n
#

with bash
# echo -n

#

Please let us know if this a bug or do we have any other option to print 
-n ?

Here is the environment details.

version: bash 4.3
Hardware and Operating System P7 AIX
Compiled with AIX xlc

Thanks,
-Sangamesh