LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Difference between "<" and "|" in shell (https://www.linuxquestions.org/questions/linux-newbie-8/difference-between-and-%7C-in-shell-307251/)

veeruk101 03-29-2005 01:24 AM

Difference between "<" and "|" in shell
 
What's the difference between using "<" and "|" when combining bash shell commands? Don't both refer to stdin, the only difference being the order? And couldn't you just enter something directly into a command rather than using both of the above? For example:

$ ls < pwd

and

$ pwd | ls

and

$ ls pwd

Does anybody know how to clearly explain the difference between all these different stdin things, and what the conceptual differences between "<" and "|" are? I'm totally new to this...

Thanks a lot!

Orkie 03-29-2005 01:40 AM

Not sure about the <.
In your 2nd example, you are taking the output of pwd and passing it to ls (e.g. if you wanted to save the output of ls, you do "ls | tee ls.log"). On the 3rd one, you are passing "pwd" as an arugment to ls. If there is a file called 'pwd' in the current working directory, then ls will display it.

mschutte 03-29-2005 03:01 AM

The three ways to run ls are completely different:

1. '<' writes the content of a FILE to the standard input of a program. Most programs behave like if you typed the content of the file manually (except programs which use a library for controlling a tty, like ncurses).

2. '|' is the pipe operator. It feeds the standard output of the left-hand side PROGRAM to the program on the right-hand side.

3. Here you pass an argument to ls.

veeruk101 03-29-2005 09:39 AM

Thanks for the replies.

mschutte, so for the second case would you say it's passing the stdout of the program on the left to the STDIN of the program on the right?

Crashed_Again 03-29-2005 11:43 AM

Quote:

Originally posted by veeruk101
Thanks for the replies.

mschutte, so for the second case would you say it's passing the stdout of the program on the left to the STDIN of the program on the right?

Exactly. As mschutte pointed out, this is known as "piping".

Also sometimes you will see something like:

# echo 'hello' >> /home/user/somefile.txt

Two >'s signify an append to the text file. If you use one > it will delete existing information in the file.


All times are GMT -5. The time now is 10:59 PM.