It seems like such a simple questions but I cant find any resource regarding it

In C/C++ , it means to store the read in a value from command prompt and store it inside a variable ,

How about in Shell Programming?

  • 9
    Close voters: This is not off topic, please stop close-voting this for the "Offtopic" reason. – Thomas Ward Jan 8 '14 at 2:40
  • 5
    Slightly off topic: It's not just an input/output in C++, its more common form is for bit shifting integers. The implementation for the i/o stream is an overridden version that applies only to streams (and other classes that override it in such a way). – Pharap Jan 8 '14 at 6:13
  • 1
    Whoops.. Meant to close this as a duplicate.. not off-topic since it is on-topic. – Seth Jan 14 '14 at 18:40
  • @Seth The timeline suggests you (and Avinash) did vote to close as duplicate but the three votes were all OT so overrode yours. The only reason I'm not voting to close is that I'm not sure the suggested duplicate is better than this one. – Oli Jan 16 '14 at 12:06
  • 1
    @Oli Well I'm not sure we should have two questions that are so similar floating around.. – Seth Jan 16 '14 at 19:52

>> can be used to pipe output into a text file and will append to any existing text in that file.

'any command' >> textfile.txt

appends the output of 'any command' to the text file.

using > will overwrite.

| improve this answer | |

The right angle bracket symbol (>) is used to redirect output to a disk file. If the file specified does not already exist, it is created; if it does exist, it is overwritten. The left angle bracket symbol (<) is used to redirect input from a disk file. To append output to an existing file, use double right angle brackets (>>)

| improve this answer | |

The > and >> are redirection operators for FD's (File Descriptors)

In bash you have tree standard FD's that are the standard input (strin), the standard output (strout) and the standard error (strerr). These can also be called by FD 0, FD 1 and FD 2 respectively.

Normally you would have you would have all FD's pointing to the terminal, but this can be changed by using redirection.

For example, if you call:

command > log.txt

You will redirect the output to the file log.txt This is similar as calling:

command 1> log.txt

As this only redirects strout you will still be able to see the error in the terminal. In order to redirect strerr to you log.txt file you will have to run:

command 2> log.txt

Again, this only redirects strerr. If you wish to redirect both stdout and stderr you need to duplicate your stderr output to stdout by using the >& command.

command 1> log.txt 2>&1

To understand this command you need to read it form right to left, first a copy of stderr is made to stdout, then strout is redirected to the log.txt file.

When you use redirection in this way, bash will not look if the file exists or not and simply create one regardless if that implies erasing the existing one. If you want to avoid loosing what has already been written in your log file you can then use the >> command in the same ways explained above, but in this case all the outputs will be appended to existing files.

For their use in C++ with cin, cout and cerr I think hash gave a better answer than I could.

I'm not an expert in these so I might have gotten some things wrong. For a more complete information I advise reading Bash guide on Greg's Wiki

| improve this answer | |

In C/C++:

In C/C++ the Left and Right Shift operators use the symbols << and >> as the bitwise operator; which perform shift operations on bits. C++ also makes the use of overloaded bitwise shift operators in basic Input/Output operations; >> and << brackets in C++ are used for extraction and insertion of data/information to streams which may be standard input/output, files.

In Shell Scripting/Programming:

In Shell Scripting/Programming, not very different from the extraction/insertion operations as mentioned above, the >> / << (variants of > / < operators) are used to redirect standard streams from / to those defined by users and perform appending (differing from > / < which overwrites) operations.


You might be interested in reading:

| improve this answer | |
  • 1
    Why do people always first talk about streams when these operators have a specific core meaning - even without any #includes? – Ruslan Jan 8 '14 at 13:00

In shell script the > operator will create a file that you will put just right to it and will erase every content of the file if exist but the >> will append the text to the file that will be right to it preferable >> will use in writing log file. like You want to add time,count or some process logs and > to create new file try this out:

  nowt=$(date +"%T")
  date1=$(date +"%s")
  NOW=$(date +"%d-%m-%Y")
  now=$NOW" "$nowt
  echo "START TIME WAS :$now " | cat > /home/user/Desktop/$file_name
  sh some_sh_file you want to run  2>&1 | tee >> /home/user/Desktop/$file_name #want to log the out put
  nowt=$(date +"%T")
  date2=$(date +"%s")
  NOW=$(date +"%d-%m-%Y")
  now=$NOW" "$NOW
  echo "$(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed."
  echo "END TIME WAS :$now" | cat >> /home/user/Desktop/$file_name
  echo "$(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed." | cat >> /home/user/Desktop/$file_name  
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.