Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Eliah Kagan, Radu Rădeanu, BuZZ-dEE, guntbert, Eric Carvalho Jan 18 at 15:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Close voters: This is not off topic, please stop close-voting this for the "Offtopic" reason. –  Thomas W. Jan 8 at 2:40
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 at 6:13
Whoops.. Meant to close this as a duplicate.. not off-topic since it is on-topic. –  Seth Jan 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 at 12:06
@Oli Well I'm not sure we should have two questions that are so similar floating around.. –  Seth Jan 16 at 19:52
add comment

5 Answers 5

>> 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.

share|improve this answer
It is the concept of Redirection. –  Ivan Chau Jan 8 at 7:33
add comment

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 (>>)

share|improve this answer
add comment

In C/C++:

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

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:

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

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

share|improve this answer
add comment

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 file_name=$NOW"_"$nowt".log" 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 diff=$(($date2-$date1)) 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

share|improve this answer
add comment

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