LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   internal working of echo command (https://www.linuxquestions.org/questions/linux-general-1/internal-working-of-echo-command-773938/)

mohit_parihar 12-07-2009 04:46 AM

internal working of echo command
 
can i know how does an echo command works internally. offcourse it is generally used to print a msg. how does it calls the msg

timmeke 12-07-2009 05:38 AM

1/ Why would you need to know this?
2/ I'm sure you can dive into the sources - typically by getting a copy of the "developer" release of the package that contains your "echo".
3/ Which Linux distribution are you running?

mohit_parihar 12-08-2009 03:46 AM

reason for knowing the internal working of echo command
 
sir actually i am under going a training on linux. In which we were asigned tasks. the question which i asked is one of my task and can't find a proper answer for this question on net. I would be thank full if u can help me with this task.

timmeke 12-08-2009 05:08 AM

These Forums are not the right place to assist with homework or similar tasks.
Pay attention to the course and you will find the answer...

catkin 12-08-2009 05:12 AM

When you write "how does it calls the msg" do you mean "where does it get the message from" or "where does it output the message to"?

Aquarius_Girl 12-08-2009 05:20 AM

Quote:

Originally Posted by mohit_parihar
can i know how does an echo command works internally. offcourse it is generally used to print a msg. how does it calls the msg

Issue the following command on a terminal:

Code:

strace echo
U will see a list of system calls that get invoked in a sequential order on executing echo command. See if that helps you !

catkin 12-08-2009 05:47 AM

Quote:

Originally Posted by anishakaul (Post 3783833)
Issue the following command on a terminal:

Code:

strace echo
U will see a list of system calls that get invoked in a sequential order on executing echo command. May be that will help !

Out of curiosity I did so. It ran the external command "echo" rather than the shell built-in (of course). Fascinating output clearly showing . . . how much I don't know about system calls :confused: I guess *n*x has moved on since Richard Stevens wrote the wonderfully lucid "Advanced Programming in the UNIX Environment"

Aquarius_Girl 12-08-2009 05:53 AM

Quote:

Originally Posted by catkin
It ran the external command "echo" rather than the shell built-in (of course).

It might seem a dumb question, but still I want to know that how did u find out that it was an external command instead of the shell built-in ?

catkin 12-08-2009 06:08 AM

Quote:

Originally Posted by anishakaul (Post 3783872)
It might seem a dumb question, but still I want to know that how did u find out that it was an external command instead of the shell built-in ?

I happen to know that echo is a built-in but could have found out using this at a ommand prompt
Code:

type echo
The strace output included this line showing it was running the external command /usr/bin/echo
Code:

execve("/usr/bin/echo", ["echo", "$"], [/* 30 vars */]) = 0

Aquarius_Girl 12-08-2009 06:22 AM

Quote:

Originally Posted by catkin (Post 3783889)
The strace output included this line showing it was running the external command /usr/bin/echo
Code:

execve("/usr/bin/echo", ["echo", "$"], [/* 30 vars */]) = 0

Thanks for the reply!

I didn't pay attention to the line pointed out by u due to my absent-mindedness :D

mohit_parihar 12-09-2009 02:37 AM

for both

Aquarius_Girl 12-09-2009 02:41 AM

Quote:

Originally Posted by mohit_parihar (Post 3785125)
for both

What was this statement ?

mohit_parihar 12-11-2009 04:42 AM

mean "where does it get the message from" or "where does it output the message to"?

catkin 12-11-2009 04:51 AM

It gets the message from the command line and sends it to stdout. Here's from echo's man page
Code:

DESCRIPTION
      Echo the STRING(s) to standard output.


Aquarius_Girl 12-11-2009 05:09 AM

Quote:

Originally Posted by mohit_parihar
how does it calls the msg

As catkin said, echo doesn't call the message, it receives the message an input from the user. Its default job is to send the received message to the standard output, but you can redirect its output to some file of your choice !

e.g:
Code:

echo "mohit_parihar" >> /home/mohit/Desktop/temp.c
Here the input string i.e. mohit_parihar will be appended at the end of the file temp.c located on your desktop folder!


All times are GMT -5. The time now is 12:21 PM.