LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-05-2008, 06:26 PM   #1
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
echo: command or built-in?


Just out of curiosity, I wonder if the echo statement is a command or a shell built-in. Indeed, it is a shell built-in... but it has also its own executable. The type built-in confirms:
Code:
# type -a echo
echo is a shell builtin
echo is /bin/echo
and the Advanced Bash Scripting Guide, too
Quote:
This command is a shell builtin, and not the same as /bin/echo, although its behavior is similar.
Here are my questions: if the behavior is similar in what they differ? And when I put an echo statement (without path) in a BASH script which one is executed, the built-in or the command?

Or in other words: who wrote the source of the command /bin/echo and why?
 
Old 01-05-2008, 06:58 PM   #2
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
I am guessing the reason is evolution as shells like the original sh did not have echo built in; hence, it was developed as a standalone tool., then, of course bash can along and rendered the command line echo redundant.

The /bin/echo utility is developed by GNU according /bin/echo --help
 
Old 01-06-2008, 05:51 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Original Poster
Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Thanks Harry, your guess sounds reasonable. I found a little history of the echo command through the various UNIX shells, here. It looks like the original Bourne shell written for Unix Version 7 didn't have echo as a built-in. Steve Bourne himself in the official documentation on 1978 states:
Quote:
echo is a standard UNIX command that prints its arguments, separated by blanks
Also, now I see that the shell uses its own built-in, since obviously it takes precedence over an external command. You gave me the correct suggestion to demonstrate: if I type
Code:
echo --help
it simply echoes the string "--help", whereas the external command /bin/echo prints out the usage.

Thank you again. Ciao,
Alex
 
Old 01-06-2008, 06:08 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

There's at least one more 'statement' that has a build-in and a binary version: pwd

They do not act the same in all situations (not sure if the same is true for echo).

Here's an example of pwd behaving different:
Quote:
$ mkdir foo
$ cd foo/
$ pwd
/home/druuna/foo

$ /bin/pwd
/home/druuna/foo

$ rm -rf ../foo/

$ pwd
/home/druuna/foo

$ /bin/pwd
/bin/pwd: couldn't find directory entry in `..' with matching i-node
Both have their use depending on what you want to do.
 
Old 01-06-2008, 06:39 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Original Poster
Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi druuna, it's correct. /bin/pwd as /bin/echo are supplied by the GNU coreutils. At least in this case the manual page states explicitly:
Quote:
NOTE: your shell may have its own version of pwd, which usually supersedes
the version described here.
Thanks for pointing this out.
 
Old 01-06-2008, 11:55 AM   #6
xptools
Member
 
Registered: Sep 2005
Location: Canada
Distribution: Debian Testing
Posts: 40

Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Just out of curiosity, I wonder if the echo statement is a command or a shell built-in. . . . if the behavior is similar in what they differ?
One thing worthwhile to mention is that when using the shell built-in echo, you can avoid the "Argument list too long" errors

You can check the following articles as well.

HTH
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo command s_hcl Linux - General 1 09-06-2006 05:27 AM
Security threat with the echo command uma_mahesh_2005 Linux - Security 8 05-04-2006 06:14 AM
what is the echo command in perl? hamish Programming 5 12-14-2004 08:31 AM
built in echo ej25 Programming 2 12-11-2004 11:01 PM
echo command peculiarity eallen Linux - General 3 02-11-2003 05:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration