$ printenv $ exportPerhaps the most important of these is the PATH variable, which specifies the directories in which an executable file can be found. You can see it's current value by doing:
$ printenv PATH $ echo $PATH
$ aliasEnvironment variables, functions and shell settings can be seen by running the set command. The output of set is quite large, so you probably want to page through it:
$ set | more (type "q" to stop paging)
Examples of settings which are not environment variables are:
PS1: initial shell prompt LS_COLORS: colorization codes used for file listing SHELLOPTS: interactive shell options COLUMNS: number of columns in terminal (80)One can contrast, say, PATH to PS1 by executing:
$ echo $PS1 (should print 80) $ printenv PS1 (nothing)
$ sudo gedit /etc/profile.d/local.sh &If a file starts with "~", it means it resides in your home directory and you do not want to be root to edit it. For example, the instructions below say:
$ gedit ~/.bashrc &
# add home-bin to front of PATH PATH=$HOME/bin:$PATH # add "." to the back of PATH # there are some security issues, so we'll avoid having it for root # if the user activating this script is root, "id -u" will equal "0" # MAKE SURE THE BRACKETS "[" and "]" DO NOT TOUCH THE INSIDE EXPRESSION [ $(id -u) -ne 0 ] && PATH=$PATH:. # optional: # set default system-wide EDITOR to nano if you cannot deal with vim # this is useful for a number of administrative commands which # invoke an editor automatically: sudoedit, visudo, edquota #EDITOR=nano
# listing aliases alias ll='ls -la' alias la='ls -A' alias lld='ls -ld' alias l='ls -CF' # force querying for potentially destructive operations alias rm='rm -i' alias mv='mv -i' alias cp='cp -i' alias path='printenv PATH' alias more='less' # less is a better (poorly named) pager than more # augments the SHELLOPTS setting with the "noclobber" option # so that output redirection to an existing file with ">" is # prevented and must be overwritten with ">|" set -C
#if [ -f ~/.bash_aliases ]; then # . ~/.bash_aliases #fiUncomment these 3 lines and add others above them, getting:
if [ -f /etc/bash_aliases ]; then
. /etc/bash_aliases
fi
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# start a root shell via the "best" method
# feel free to choose a name other than besu
alias besu='sudo -H bash -l'
# this sets the title for the terminal bar correctly
# for the terminal shell application
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}\007"'
# this is a convenience for editing "your" files with gedit, where
# we want the invocation for the FIRST file, file1, to be:
# gedit file1 &
# and the invocation of all other files to be:
# gedit file
e() {
# the "ps" part prints users of all gedit processes; the "grep" part
# selects sends "success" if there are any for me "id -un"
if ps -C gedit -o user= | grep -q $(id -un); then
# later gedit invocations
gedit "$@"
else
# first gedit invocation
gedit "$@" &
fi
}
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
We acquire the home-bin portion of the PATH
from the global environment.
$ mkdir ~/bin
$ echo $TERMIt clearly supports color, but the ~/.bashrc script doesn't turn color on automatically. Edit ~/.bashrc and look for these lines:
# uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt #force_color_prompt=yesAs suggested, uncomment the last line:
force_color_prompt=yesThe actual color settings are done later in this block:
if [ "$color_prompt" = yes ]; then
PS1=...
else
PS1=...
fi
Color/style settings are created by the "special strings" like
"\[\033[01;32m\]" which you see in the prompt definition.
Some alternative color/styles you can try are these:
\[\033[01;31m\] (bold/red) \[\033[01;34m\] (normal/reddish)Just make sure that the last color change string is "\[\033[00m\]".
if [ "$color_prompt" = yes ]; then
PS1='... \w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Replacing the occurrences of
"\w" by "\W" (lower case to upper case)
will show only the basename of
the current working directory.
Another common alternative, similar to the Windows DOS shell prompt,
is to make the prompt complete on the following line
after the full directory is printed.
Do this by inserting a newline character "\n"
just before the end of the prompt string, i.e, make
"\$" into "\n\$".
$ alias -- you should see the aliases listed here $ path /home/LOGIN/bin:/usr/local/sbin: /usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:. ------------------ we want to observe the "e()" function: $ set | more type: /^e followed by Enter, replacing the ":" type q to leave pager
$ sudo mkdir /root/bin
case "$TERM" in
xterm-color)
PS1=...
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
;;
esac
and change the case test to:
xterm*)
$ sudo cp ~/.bashrc ~/.profile /etc/skel $ sudo mkdir /etc/skel/bin
$ path Observe home-bin and . in PATH $ sudo su # path no /root/bin in PATH # pwd working directory unchanged # exit $ sudo su - # path PATH has /root/bin # pwd working directory is /root # exit $ besu # path PATH has /root/bin # pwd working directory unchanged # exit
$ bash something.shIf the script is executable, you can omit the "bash" call. Unlike calling bash by itself, this is a non-interactive shell. When you want to use a script like a program, what you want is to execute it. The script is run in a subshell and has no effect on the environment of the current shell.
In contrast, sourcing executes the statements, but the purpose of sourcing a bash file is primarily to augment the environment. The exported variables are created by the shell command
export VARIABLEUsually the export statement is combined with the variable's definition, like
export VARIABLE=valuewhich is equivalent to these two statements (in either order):
export VARIABLE VARIABLE=valueSyntactically, sourcing the file something.sh is done in either of these two synonymous ways:
$ source something.shor
$ . something.shHere is an example. Use an editor to create these two simple scripts:
$ bash setVars.sh execution: variables AA, BB set in a subshell $ echo $AA $BB AA, BB undefined $ . setVars.sh source: variables set in current shell $ echo $AA $BB AA, BB now defined $ . showVars.sh source: both variables appear $ bash showVars.sh execution: only the exported one appears
$ path (shows value of PATH)
Then, for example, suppose you define this one-line script
named showPath.sh:
pathThe execution will fail:
$ bash showPath.shwhereas sourcing will succeed:
$ . showPath.sh
/etc/passwdThe login shell created obtains its initial environment from:
/etc/environmentand then augments the environment by sourcing these two files:
/etc/profile
~/.profile (or ~/.bash_profile or ~/.bash_login supercede, if existing)
Other files contribute to the environment by being sourced explicitly
in these two files.
For Ubuntu, these additional sourcings are used:
/etc/profile sources /etc/profile.d/*.sh /etc/profile sources /etc/bash.bashrc if the shell is interactive ~/.profile sources ~/.bashrc if the shell is interactive
/etc/profile.d/local.shYou can add other .sh files or use this one only.