What is the difference between using "echo" command and editing using "vi" when making changes to .bashrc file?
vi
is an editor. It does what editors usually do: You can modify a file in random places, moving the cursor around to do that.
Modifying a file with echo
will alwas add text to the end of the file; or overwrite it completely. The changes are just as permanent as using an editor like vi
.
echo "foo" >.bashrc
This replaces all of that file with just one line "foo". You probably don't want that.
echo "foo" >>.bashrc
This adds a new line "foo" to the end of .bashrc. While that may be useful sometimes, normally you want more control of where your modifications go, so better use an editor.
And it's the shell that does all that magic: That >
redirects the output of a command to a file, overwriting any old content of that file in the process; >>
is similar but it does not overwrite old content, it only adds new content to the end.
Tutorials use echo
typically because it's a very simple command that just - you might have guessed it - echos its arguments. But that I/O redirection with >
or >>
works with any command that writes output to the standard output channel (a.k.a. stdio).