14
3
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?
14
3
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?
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 – 2018-06-28T15:14:39.180
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 – 2018-06-29T16:23:30.250
@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 – 2018-06-30T22:20:34.543
@kasperd You can also scatter stdin redirection in between in any order, additionally. – rexkogitans – 2018-07-01T08:00:10.840
48
>>
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.
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 – 2018-07-02T02:27:54.847
-2
Again, if you do
> somefile.txt
The entire content of the file will be cleared.
Why are you copying other answer? – Toto – 2018-06-30T15:18:44.337
@Toto I do not think he does. It seems to be aditional, and very valuable. – Volker Siegel – 2018-06-30T15:51:28.560
@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 – 2018-06-30T15:58:28.840
2@VolkerSiegel: > file
clears the file but >> file
doesn't do anything. – Toto – 2018-06-30T15:59:57.090
but this doesn't answer the question in any way and should be a comment – phuclv – 2018-07-02T02:22:15.160
@Toto. First I am sorry if you feel offended by my answer. And yes, you are right I am new here. I saw this question in a thread and I thought, "oh let me help this person out" and I think that is the reason this platform does exist. For sharing. And if you think I am copying someone's answer, tell me which of them look as simple and straight to the point as mine. So I made a typo of adding another > to >, which I have corrected. So whats the backlash? I am not taking this personally. I just want you to know that first, this place is for helping not backlashing. cheers! – rocksyne – 2018-07-02T02:29:43.017
26
Just to let you know what to improve in the future:
– Kamil Maciorowski – 2018-06-28T13:01:42.007man 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.5
For a detailed overview of command-line redirection operators, see https://unix.stackexchange.com/q/159513/85039
– Sergiy Kolodyazhnyy – 2018-06-28T18:49:24.343