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?
Asked
Active
Viewed 3,149 times
3
-
2You 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? – vpit3833 Jul 11 '12 at 06:55
-
@vpit3833 A natural reason for modifying `.bashrc` variables through script is when creating an installation script, for which symlinks cannot be created. – Dr_Zaszuś Jul 07 '20 at 07:35
4 Answers
1
Yes, you write a script that appends to .bashrc with the >> redirection operator:
echo Your Text >> ~/.bashrc
See also I/O Redirection
Wernsey
- 5,362
- 21
- 38
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.
tuxuday
- 2,843
- 16
- 18
-
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). – Dr_Zaszuś Jul 07 '20 at 07:37
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.
Dennis Williamson
- 324,833
- 88
- 366
- 429