11

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?

  • 26
    Just to let you know what to improve in the future: man bash is written with quite a formal language, it may be hard to understand. On the other hand the phrase bash >> returns few useful links in Google. It's true we want to aggregate knowledge without relying on Google; for this reason even a question with easily searchable answer may fit. However the fact you neither provided the answer right away (see Can I answer my own question?) nor pointed to existing resources (that you possibly need help to understand) indicates a lack of research. – Kamil Maciorowski Jun 28 '18 at 13:01
  • 5
    For a detailed overview of command-line redirection operators, see unix.stackexchange.com/q/159513/85039 – Sergiy Kolodyazhnyy Jun 28 '18 at 18:49
31

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

  • 28
    "program before" What does that mean? Command obviously, but redirections can also be written prepended, i.e. >> file command – rexkogitans Jun 28 '18 at 15:14
  • An example. On Ubuntu 18.04 I could type "apt search rust >> x" The "apt search rust" part searches available packages for the term rust and then sends this to standard out (normally the terminal in this case). However the ">> x" says to redirect the output to the end of file "x". If I just wanted to replace the file, I would only need a single ">". – NomadMaker Jun 29 '18 at 16:23
  • @rexkogitans Over even in the middle of the command if you want to make your code confusing to read. echo a b >> c d would append a b d to a file named c. – kasperd Jun 30 '18 at 22:20
  • @kasperd You can also scatter stdin redirection in between in any order, additionally. – rexkogitans Jul 1 '18 at 8:00
48

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.

7

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
  • I would recommend you add an example of echo Dino > flintstones.txt to show the contents of it would be simply Dino if done with a single right arrow. – JakeGould Jul 2 '18 at 2:27
-2

Again, if you do

> somefile.txt

The entire content of the file will be cleared.

  • Why are you copying other answer? – Toto Jun 30 '18 at 15:18
  • @Toto I do not think he does. It seems to be aditional, and very valuable. – Volker Siegel Jun 30 '18 at 15:51
  • @Toto Oops! Looking closer, it's actually not the case. Wrong even in multiple aspects. Odd - he's new here, but active elsewhere - so probably not just trolling. – Volker Siegel Jun 30 '18 at 15:58
  • 2
    @VolkerSiegel: > file clears the file but >> file doesn't do anything. – Toto Jun 30 '18 at 15:59
  • but this doesn't answer the question in any way and should be a comment – phuclv Jul 2 '18 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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