I need to add the following lines to the end of .bashrc file in Ubuntu. How can I add these lines automatically using a script. If I run the script, then it automatically inserts the lines at the end of .bashrc file.

export APP="/opt/tinyos-2.x/apps"
export TOS="/opt/tinyos-2.x/tos"
  • @deenaik post your edit as new question. Do not hijack existing, answered questions.
    – muru
    Apr 4, 2017 at 10:31

1 Answer 1


Use the echo command to append lines to the .bashrc script

So in your script use these lines

echo 'export APP=/opt/tinyos-2.x/apps' >> ~/.bashrc 

echo  'export TOS=/opt/tinyos-2.x/tos' >> ~/.bashrc 

Make sure to use >> (append), if you use a single > you will overwrite the file.

~/.bashrc indicates .bashrc is in your home directory

  • When I do this it appends the echoed string to the last line already in the bashrc file; as in, not on a new line and without a space on the current last line...
    – d8aninja
    Sep 17, 2018 at 17:16
  • 1
    @d8aninja, this would happen if the last line of your file was not terminated with EOL character (0x0A).
    – Alexey
    Nov 7, 2018 at 9:30
  • What if we want to remove or update a variable from that file?
    – Maf
    Sep 22, 2021 at 17:57

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.