Score:0

What is the difference between using "echo" command and editing using "vi" when making changes to .bashrc file?

cn flag

I finished installing a program and some websites tell me to use the echo command to export an environment variable to the .bashrc file while others say to use vi .bashrc to edit the file directly. I was wondering what the differences between the methods would make me use one vs. the other in any given case? I'm not sure if using echo inserts a temporary variable while vi can make it permanent?

Score:3
es flag

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).

Alex avatar
cn flag
Thank you! I was really not sure if I went with "echo" I would have to do it every time I opened a new terminal. This makes a lot of sense
Score:0
de flag

For some cases only echo xxx >> file should be used to append a line, otherwise may cause other reader fail (e.g when use vim). e.g when use kafka-connect to load a file into topic.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.