[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Two bash programming questions (fwd)

From: Michael Schwendt
Subject: RE: Two bash programming questions (fwd)
Date: Mon, 11 Jun 2001 08:02:47 +0200 (CEST)

On 2001-06-10, Bob Proulx wrote:

>> $ rpm -qf `which echo`
>> sh-utils-2.0-13
>The 'which' command will always report external commands.  But the
>shell has internal replacements for many external commands.  The
>'echo' command is one of those that is typically an internal shell
>command unless you force it otherwise.

Yes. I should have mentioned that I had tried it with /bin/echo
explicitly before thinking it could be a misbehaviour.

It has been my silent assumption that both implementations would
behave the same (for sake of compatibility).

>Try this.
>  echo echo -en "\\\b"
>This prints "echo -en \\b".  Which shows the shell is interpreting the
>quoted string.  And the command will, of course, interpret the string
>too.  Which means you really need two sets of quotes for characters
>that are special to both the shell and to the command, such as the \

Oh well, that makes sense. I should have thought about that myself.

>Try this for what you want.
>  echo -en "\\\\\b"

I've chosen

  echo -en '\\\b'

now to aid readability.

>For portable scripts I recommend that you avoid 'echo' when doing
>either metacharacters or avoiding newline printing and use the POSIX
>defined 'printf' command instead.  It is also built into most shells
>and so the performance will be equivalent to echo.  But the interface
>definition is portable to all POSIX systems.  And it is always
>available in sh-utils as well so even if a shell did not provide it as
>a built in it is available externally.  [I still use the traditonal
>echo for everything else but metacharacters and newline avoidance.  It
>is the traditional method of printing in shell scripts.]
>Using printf your example would be:
>  printf "\\\\\b"

Good tip.

  printf "%s\b" "\\"

Thanks for a long reply.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]