What does `>>` mean in terminal command?

I came across a command just now given below -

$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

From what i can guess, it is probably used for committing the changes in .bash_profile but what exactly is it used for?


It redirects the stdout of the program before >> and appends it to the given file after.


Short answer — what does >> do?

With >>, you append the output of a command to a file.

Your example command consists of several parts, basically:

command >> filename

So the output of command would be appended to filename.


What happens in the specific case of echo?

In your specific case, the echo "…" command outputs its input arguments to “stdout”, which is the so-called “standard output descriptor”. The input arguments to echo are followed by a newline (\n), so that you get a line break.

Here, a “standard output descriptor” is nothing more than an output stream that is shown in your shell when you execute a command. (That is, when you type echo foo and hit Enter, foo\n is the actual output of the echo command, which is shown by your shell as foo followed by a newline.)

Basically anything that writes to your command line is using stdout. There is also another descriptor called “stderr” that is typically used for error messages. It will also be printed like stdout, so sometimes they could be interspersed. And there is a stdin descriptor that is used for input. See this article for more info.

How do you redirect output in the shell?

You can always redirect stdout to a file descriptor, which you can do with one of these operators:

  • > redirects to a file descriptor. It creates the file if it does not exist, or, if it already exists, truncates the file before writing. The file will be therefore overwritten with stdout.

  • >> appends to a file descriptor. It creates the file if it does not exist.

You can also redirect stderr by using 2> or 2>> in a similar fashion. Or you can combine stderr and stdout into one file: 2>&1 does that. For more info about redirection and some more examples, you can read this small tutorial.

How can I figure out what a particular piece of shell code means?

Generally, you may want to try explainshell.com, which will give you visual guidance and information about a particular shell command.


For the TLDR people who just like to see an example;

The standard output (not errors) of the command before the >> will get added to the end of the file named after it.

So if file "flintstones.txt" contains;

Fred
Barney

echo Dino >> flintstones.txt will result in 'Dino' being added to the end of the file;

Fred
Barney
Dino