This is a general procedure you can use for pretty much any shell. In any case, you have to know which shell the user would normally log in with:
path="$(grep $USER /etc/passwd | cut -d ':' -f 7)"
shell="$(basename -- "$path")"
Then you have to figure out which dot-files this shell would normally read:
man $shell
A shortcut which might work is to list those dot-files which contain the shell name:
ls ~/.*${shell}*
If you want to check if one of the files is actually read during login, you can simply print the file name in each of them, for example:
echo .bashrc
When logging in, you should then see which files are being read, and you can decide which one to modify. Beware that you should not to try to use echo "$0"
or similar, because the value of $0
depends on how the shell processes dot-files, and could be misleading.
When it comes to declaring the variable "permanently", note that this only extends to the session. There is no way to access the value of a variable without a session, so it has no meaning outside of one. If you mean "read-only", that is shell dependent, and in Bash you can use:
declare -r VAR
if it already has a value, or
declare -r VAR=value
to assign it at the same time. Not all shells have this feature.
To declare a variable in most shells, you should use a variable name ([A-Za-z_][A-Za-z0-9_]*
), followed by an equal sign (and no spaces around the equal sign), then a value (preferably quoted unless the value is a simple [A-Za-z0-9_]+
). For example:
name="John Doe"
ip=127.0.0.1
HORRIBLE=1
.bashrc
is the main place for that. – rozcietrzewiacz Sep 28 '11 at 11:58.bash_profile
contains something like[[ -f ~/.bashrc ]] && . ~/.bashrc
. – rozcietrzewiacz Sep 28 '11 at 12:38.bashrc
show but not the ones in.bash_profile
! ok, easy fix but... i dont understand why this is happening. – Sep 28 '11 at 12:40.bashrc
; if it's not, put it in.profile
. I source.profile
and.bashrc
(in that order) from.bash_profile
, and make sure to put[[ $- != *i* ]] && return
on the top of.bashrc
. – Alexia Luna Nov 06 '14 at 14:50