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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityIt redirects the stdout of the program before >>
and appends it to the given file after.
>> file command
Jun 28, 2018 at 15:14
echo a b >> c d
would append a b d
to a file named c
.
>>
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
.
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.
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.
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
echo Dino > flintstones.txt
to show the contents of it would be simply Dino
if done with a single right arrow.
Jul 2, 2018 at 2:27
Again, if you do
> somefile.txt
The entire content of the file will be cleared.
man bash
is written with quite a formal language, it may be hard to understand. On the other hand the phrasebash >>
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.