A "variable stored in a file" isn't really a thing. Most kinds of variables exist in the state of a running process (shell or other program). (There's a difference between environment variables, shell variables, etc, but that's not important here.) Each process has its own set of variables (except that copies of environment variables get inherited by subprocesses when they're created).
You can define a variable in a shell process with a command like name="Jhon"
, but it only defines that variable in that particular shell process, not any other. Also, it's an executable command; the definition only takes place when it's executed (not e.g. when it's sitting in a file), and can be changed later by running some other command like name="Rho"
or unset name
.
The usual way to define a variable "everywhere" is to put a command to define it in a shell initialization file, like ~/.bashrc. But this definition only gets executed if the shell runs commands from that file when it starts up; for instance, a bash login bash shell will run ~/.bash_profile (or ~/.bash_login or ~/.profile) instead of ~/.bashrc; shells other than bash (like zsh and dash) will run still different init files.
name="Jhon"
line how do you expect the value to be set? No matter how it is done, that line must be run for the variable to be set. One way issource
as you have mentioned. If you want to do it for each shell invocation then put it into your shell's rc file. e.g.~/.bashrc
for bash shell. – kaylum Jan 05 '20 at 03:00