3

I need to write a shell script to set environment variable and modify path variable permanently. Is there in any way to modify bashrc file through script?

2
  • 2
    You can edit the bashrc or you can call your script that sets the variables from bashrc. If you run the script as in . script_to_set_vars, the vars will last for that session only. Why would you want to modify bashrc through script? Commented Jul 11, 2012 at 6:55
  • @vpit3833 A natural reason for modifying .bashrc variables through script is when creating an installation script, for which symlinks cannot be created. Commented Jul 7, 2020 at 7:35

4 Answers 4

1

Yes, you write a script that appends to .bashrc with the >> redirection operator:

echo Your Text >> ~/.bashrc

See also I/O Redirection

Sign up to request clarification or add additional context in comments.

1 Comment

This will not update the variables already present in .bashrc.
0

Is there in any way to modify bashrc file through script?, you don't need to do that.

Just manually set the required variable in .bashrc once that will do.

1 Comment

A natural reason for modifying .bashrc variables through script is when creating an installation script, for which symlinks cannot be created. Instead of asking the user to modify the variables in the script, it could be much easier to do it by launching a script (in theory).
0

I would prefer modifying the .bashrc file directly

Comments

0

Your question is too general. You should specify what kind of changes you need to make. What some answerers don't seem to imagine is that you might need to script this in order to make changes to multiple accounts on a system, for example.

In general, you can use any technique that you would use on any other text file. One way would be to use sed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.