Re: Printing "-n" with the echo command
[email protected] (Bob Proulx) Wed, 21 Jan 2004 10:34:22 -0700
| Newsgroups | gmane.comp.gnu.sh-utils.bugs |
|---|---|
| Message-ID | <[email protected]> |
[email protected] wrote: > We use the command "echo" in program building scripts. You are probably using the shell built-in echo in that case. Which shell are you using for your scripts? Hopefully #!/bin/sh but which shell is that on your system? The possibilities are many and include ash, bash, ksh, old bourne shell, or a native POSIX shell implementation such as on some commercial systems. On my system I can do the following and see that it is a shell builtin. type echo echo is a shell builtin > When echoing build > parameters as "-n" alone, the characters "-n" are never echoed and by the > way are lost. > Ex. > > TOTO="-n" > > echo "$TOTO" > > echo "-n" > > echo "-n -a" > -n -a Since I am looking at bash's built-in version I can use its online help system. help echo echo: echo [-neE] [arg ...] Output the ARGs. If -n is specified, the trailing newline is suppressed. But that is not portable. Better to use 'printf' instead. If I recall correctly at one time System V systems (known as USG echo) always interpreted escape sequences such as \c for don't print a newline and never used the -n -e options. BSD systems on the other hand wrote a different echo command which did not interpret escape sequences without -e and used -n to avoid printing the newline. Ack! Why couldn't they have converged years ago to a common standard? So the flavor of command you like depends on whether you want the SysV flavor or the BSD flavor. POSIX specifies the SysV behavior but most Linux distributions go with the bash default and the bash default is BSD behavior in violation of the standard. Meanwhile FreeBSD at least is now using a POSIX standard shell so BSD-like is no longer a good description since BSD today is POSIX in this regard. This is all off of the top of my head and I probably have ten details wrong, sorry, but you can see the flamefest that you have stepped into by this. That was all that I intended by this paragraph. > I understand "-n" is an option of the "echo" command, but could it be > possible to print it as regular text when placed between quotes ? If you want to print a literal -n at the front of the string then it is better to use printf instead. This problem with echo has been around for years and I would not expect it to be cleared up any time soon. printf -- "-n\n" Quotes are processed by the shell. Commands invoked from the shell do not see any quotes. So the answer is no that does not work. You could write a new shell that did a different style of quoting. But that would be a lot of work and would be completely different than anyone else. So it is better to understand shell quoting. > PS. We use the shell command "echo" of the RedHat V7.3 distribution In that case the 'echo' you are experiencing is from bash-2.05a or very close to that version. I could not really say since I am using Debian GNU/Linux myself. Since different people use different distributions of the same software it is always best to state the version of the software you are talking about rather than who distributed it. Saying Red Hat 7.3 means nothing in this context, especially to someone who does not have that particular variant around to go look. But saying bash-2.05a means exactly a particular version which anyone can know on many different systems. You can usually get the version from commands by asking the command with the --version option such as here. bash --version GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu) I say usually because the built-in echo does not work this way. But the echo supplied with coreutils does. echo --version --version /bin/echo --version echo (GNU coreutils) 5.0.91 [coreutils is the newer version of sh-utils and also includes fileutils and textutils.] So thank you for reporting the problem. But as you can see we over here are the /bin/echo folks and not the shell built-in folks. But also as you can see they do behave the same so it did not matter. But you are stuck. Use printf instead. Bob