Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  31] [ 0]  / answers: 1 / hits: 123606  / 1 Year ago, sun, april 2, 2023, 3:36:20

Normally we can source ~/.bashrc file using this command



source ~/.bashrc


but if I write this in a shell script and execute it, nothing happens. Why?

Is there any way to do this?



My script:



#!/bin/bash
chmod a+x ~/.bashrc
source ~/.bashrc


Also tried . (dot) instead of source. Same result.


More From » bash

 Answers
1

A shell script is run in its own shell instance. All the variable settings, function definitions and such only affect this instance (and maybe its children) but not the calling shell so they are gone after the script finished.



By contrast the source command doesn't start a new shell instance but uses the current shell so the changes remain.



If you want a shortcut to read your .bashrc use a shell function or an alias instead of a shell script, like



alias brc='source ~/.bashrc'

[#43201] Monday, April 3, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
odyroc

Total Points: 324
Total Questions: 109
Total Answers: 103

Location: Belize
Member since Mon, Apr 17, 2023
1 Year ago
;