http://www.cyberciti.biz/howto/shell-primer-configuring-your-linux-unix-osx-environment
On a Linux or Unix-like systems each user and process runs in a specific environment. An environment includes variables, settings, aliases, functions and more. Following is a very brief introduction to some useful shell environment commands, including examples of how to use each command and setup your own environment to increase productivity in the command prompt.
![bash-shell-welcome-image]()
The /etc/shells file contains a list of the shells on the system. For each shell a single line should be present, consisting of the shell's path, relative to root. Type the following cat command to see shell database:
![Fig.04: Common bash environment variables]()
To look at all your bash config files, enter:
![BashStartupfiles]()
Depending on which shell is set up as your default, your user profile or system profile can be one of the following:
You can reuses commands. Simply hit [Up] and [Down] arrow keys to see previous commands. Press [CTRL-r] from the shell prompt to search backwards through history buffer or file for a command. To repeat last command just type !! at a shell prompt:
On a Linux or Unix-like systems each user and process runs in a specific environment. An environment includes variables, settings, aliases, functions and more. Following is a very brief introduction to some useful shell environment commands, including examples of how to use each command and setup your own environment to increase productivity in the command prompt.

Finding out your current shell
Type any one of the following command at the Terminal app:ps $$OR
ps -p $$
echo"$0"Sample outputs:
Finding out installed shells
To find out the full path for installed shell type:type -a zshSample outputs:
type -a ksh
type -a sh
type -a bash
The /etc/shells file contains a list of the shells on the system. For each shell a single line should be present, consisting of the shell's path, relative to root. Type the following cat command to see shell database:
cat /etc/shells
Sample outputs:# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/fish
Changing your current shell temporarily
Just type the shell name. In this example, I'm changing from bash to zsh:zsh
You just changed your shell temporarily to zsh. Also known as subshell. To exit from subshell/temporary shell, type the following command or hit CTRL-d:exit
Finding out subshell level/temporary shell nesting level
The $SHLVL incremented by one each time an instance of bash is started. Type the following command:echo"$SHLVL"Sample outputs:
Changing your current shell permanently with chsh command
Want to change your own shell from bash to zsh permanently? Try:chsh -s /bin/zshWant to change the other user's shell from bash to ksh permanently? Try:
sudochsh -s /bin/ksh
Finding out your current environment
You need to use theenvSample outputs:
env | more
env | less
env | grep'NAME'
TERM_PROGRAM=Apple_TerminalHere is a table of commonly used bash shell variables:
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/6x/45252d6j1lqbtyy_xt62h40c0000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-djaOJg/Render
TERM_PROGRAM_VERSION=326
TERM_SESSION_ID=16F470E3-501C-498E-B315-D70E538DA825
USER=vivek
SSH_AUTH_SOCK=/tmp/launch-uQGJ2h/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0:0
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/go/bin:/usr/local/sbin/modemZapp:/Users/vivek/google-cloud-sdk/bin
__CHECKFIX1436934=1
PWD=/Users/vivek
SHLVL=2
HOME=/Users/vivek
LOGNAME=vivek
LC_CTYPE=UTF-8
DISPLAY=/tmp/launch-6hNAhh/org.macosforge.xquartz:0
_=/usr/bin/env
OLDPWD=/Users/vivek

Fig.04: Common bash environment variables
Warning: It is always a good idea not to change the following environment variables. Some can be changed and may results into unstable session for you:
SHELL
UID
RANDOM
PWD
PPID
SSH_AUTH_SOCK
USER
HOME
LINENO
SHELL
UID
RANDOM
PWD
PPID
SSH_AUTH_SOCK
USER
HOME
LINENO
Displays the values of environment variables
Use any one of the following command to show the values of environment variable called HOME:## Use printenv ##Sample outputs:
printenv HOME
## or use echo ##
echo"$HOME"
# or use printffor portability ##
printf"%s\n""$HOME"
/home/vivek
Adding or setting a new variables
The syntax is as follows in bash or zsh or sh or ksh shell:## The syntax is ##Again, use the printenv or echo or printf command to see the values of environment variables called PATH, EDITOR, and TMOUT:
VAR=value
FOO=bar
## Set the default editor to vim ##
EDITOR=vim
export$EDITOR
## Set default shell timeout for security ##
TMOUT=300
export TMOUT
## You can directly use exportcommand to set the search path for commands ##
exportPATH=$PATH:$HOME/bin:/usr/local/bin:/path/to/mycoolapps
printenv PATH
echo"$EDITOR"
printf"%s\n"$TMOUT
How do I change an existing environment variables?
The syntax is as follows:exportVAR=valueThe syntax is as follows for the tcsh shell for adding or changing a variables:
## OR ##
VAR=value
export$VAR
## Change the default editor from vim to emacs ##
echo"$EDITOR"## <--- print="" span="" vim="">
EDITOR=emacs ## <--- change="" it="" span="">
export$EDITOR## <--- span="" style="color: #7a0874; font-weight: bold;">export---> it for next session too --->
echo"$EDITOR"## <--- emacs="" print="" span="">
--->--->
## Syntax
setenv var value
printenv var
## Set foo variable with bar as a value ##
setenv foo bar
echo"$foo"
printenv foo
## Set PATH variable ##
setenv PATH $PATH\:$HOME/bin
echo"$PATH"
## set PAGER variable ##
setenv PAGER most
printf"%s\n"$PAGER
Finding your bash shell configuration files
Type the following command to list your bash shell files, enter:ls -l ~/.bash* ~/.profile /etc/bash* /etc/profile
Sample output:To look at all your bash config files, enter:
less ~/.bash* ~/.profile /etc/bash* /etc/profile
You can edit bash config files one by one using the text editor such as vim or emacs:vim ~/.bashrcTo edit files located in /etc/, type:
## first make a backup.. just incase
sudocp -v /etc/bashrc /etc/bashrc.bak.22_jan_15
########################################################################
## Alright, edit it to your hearts content and by all means, have fun ##
## with your environment or just increase the productivity :) ##
########################################################################
sudo vim /etc/bashrc
Confused by Bash shell Initialization files?
The following "bash file initialization" graph will help you:
Depending on which shell is set up as your default, your user profile or system profile can be one of the following:
Finding your zsh shell configuration files
The zsh wiki recommend the following command:strings =zsh | grep zshrcSample outputs:
/etc/zshrcType the following command to list your zsh shell files, enter:
.zshrc
ls -l /etc/zsh/* /etc/profile ~/.z*To look at all your zsh config files, enter:
less /etc/zsh/* /etc/profile ~/.z*
Finding your ksh shell configuration files
- See ~/.profile or /etc/profile file.
Finding your tcsh shell configuration files
- See ~/.login, ~/.cshrc for the C shell.
- See ~/.tcshrc and ~/.cshrc for the TC shell.
Can I have a script like this execute automatically every time I login?
Yes, add your commands or aliases or other settings to ~/.bashrc (bash shell) or ~/.profile (sh/ksh/bash) or ~/.login (csh/tcsh) file.Can I have a script like this execute automatically every time I logout?
Yes, add your commands or aliases or other settings to ~/.bash_logout (bash) or ~/.logout (csh/tcsh) file.History: Getting more info about your shell session
Just type the history command to see session history:history
Sample outputs:9 lsType history 20 to see the last 20 commands from your history:
10 vi advanced-cache.php
11 cd ..
12 ls
13 w
14 cd ..
15 ls
16 pwd
17 ls
....
..
...
91 hddtemp /dev/sda
92 yum install hddtemp
93 hddtemp /dev/sda
94 hddtemp /dev/sg0
95 hddtemp /dev/sg1
96 smartctl -d ata -A /dev/sda | grep -i temperature
97 smartctl -d ata -A /dev/sg1 | grep -i temperature
98 smartctl -A /dev/sg1 | grep -i temperature
99 sensors
history20Sample outputs:
You can reuses commands. Simply hit [Up] and [Down] arrow keys to see previous commands. Press [CTRL-r] from the shell prompt to search backwards through history buffer or file for a command. To repeat last command just type !! at a shell prompt:
ls -l /foo/barTo see command #93 (hddtemp /dev/sda)from above history session, type:
!!
!93
Changing your identity with sudo or su
The syntax is as follows:su userNameSee "Linux Run Command As Another User" post for more on sudo, su and runuser commands.
## To log inas a tom user ##
su tom
## To start a new login shell for tom user ##
su tom
## To loginas root user ##
su -
## The sudocommand syntax (must be configured on your system) ##
sudo -s
sudo tom
Shell aliases
An alias is nothing but shortcut to commands.Listing aliases
Type the following command:alias
Sample outputs:alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias bc='bc -l'
alias cd..='cd ..'
alias chgrp='chgrp --preserve-root'
alias chmod='chmod --preserve-root'
alias chown='chown --preserve-root'
alias cp='cp -i'
alias dnstop='dnstop -l 5 eth1'
alias egrep='egrep --color=auto'
alias ethtool='ethtool eth1'
Create an alias
The bash/zsh syntax is:aliasc='clear'Type c alias for the system command clear, so we can type c instead of clear command to clear the screen:
aliasdown='sudo /sbin/shutdown -h now'
cOr type down to shutdown the Linux based server:
You can create as many aliases you want. See "30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X" for practical usage of aliases on Unix-like system.
down
Shell functions
Bash/ksh/zsh functions allows you further customization of your environment. In this example, I'm creating a simple bash function called memcpu() to display top 10 cpu and memory eating process:Just type memcpu to see the info on screen:
memcpu(){echo"*** Top 10 cpu eating process ***"; ps auxf | sort -nr -k 3 | head-10;
echo"*** Top 10 memory eating process ***"; ps auxf | sort -nr -k 4 | head-10; }
memcpuSee "how to write and use shell functions" for more information.
*** Top 10 cpu eating process ***
nginx 3955913.00.226402035168 ? S 04:260:00 \_ /usr/bin/php-cgi
nginx 395456.60.121648413088 ? S 04:250:04 \_ /usr/bin/php-cgi
nginx 394716.20.627335281704 ? S 04:220:17 \_ /usr/bin/php-cgi
nginx 395445.70.121648413084 ? S 04:250:03 \_ /usr/bin/php-cgi
nginx 395405.50.122126019296 ? S 04:250:04 \_ /usr/bin/php-cgi
nginx 395425.40.121648413152 ? S 04:250:04 \_ /usr/bin/php-cgi
nixcraft 395435.30.121648414096 ? S 04:250:04 \_ /usr/bin/php-cgi
nixcraft 395385.20.122124818608 ? S 04:250:04 \_ /usr/bin/php-cgi
nixcraft 395395.00.121648416272 ? S 04:250:04 \_ /usr/bin/php-cgi
nixcraft 395414.80.121648414860 ? S 04:250:04 \_ /usr/bin/php-cgi
*** Top 10 memory eating process ***
498638590.54.02429652488084 ? Ssl 2014177:41 memcached -d -p 11211 -u memcached -m 2048 -c 18288 -P /var/run/memcached/memcached.pid -l 10.10.29.68 -L
mysql 642214.23.44653600419868 ? Sl 20141360:40 \_ /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
nixcraft 394180.41.1295312138624 ? S 04:170:02 | \_ /usr/bin/php-cgi
nixcraft 394190.50.9290284113036 ? S 04:180:02 | \_ /usr/bin/php-cgi
nixcraft 394640.70.829435699200 ? S 04:200:02 | \_ /usr/bin/php-cgi
nixcraft 394690.30.728840091256 ? S 04:200:01 | \_ /usr/bin/php-cgi
nixcraft 394716.20.627335281704 ? S 04:220:17 \_ /usr/bin/php-cgi
vivek 392612.20.625317282812 ? S 04:050:28 \_ /usr/bin/php-cgi
squid 99950.00.517515272396 ? S 201427:00 \_ (squid) -f /etc/squid/squid.conf
cybercit 39220.00.430338056304 ? S Jan10 0:13 | \_ /usr/bin/php-cgi
Putting it all together: Customizing your Linux or Unix bash shell working environment
Now, you are ready to configure your environment using bash shell. I'm only covering bash. But the theory remains same from zsh, ksh and other common shells. Let us see how to adopt shell to my need as a sysadmin. Edit your ~/.bashrc file and append settings. Here are some useful configuration options for you.#1: Setting up bash path and environment variables
# Set path ##Use less or most command as a pager:
exportPATH=$PATH:/usr/local/bin:/home/vivek/bin:/opt/firefox/bin:/opt/oraapp/bin
# Also set path forcd command
exportCDPATH=.:$HOME:/var/www
exportPAGER=lessSet vim as default text editor for us:
export EDITOR=vimSet Oracle database specific stuff:
export VISUAL=vim
export SVN_EDITOR="$VISUAL"
exportORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/serverSet JAVA_HOME and other paths for java as per java version:
exportORACLE_SID=XE
exportNLS_LANG=$($ORACLE_HOME/bin/nls_lang.sh)
exportJAVA_HOME=/usr/lib/jvm/java-6-sun/jreSecure my remote SSH login using keychain for password less login:
# Add ORACLE, JAVA to PATH
exportPATH=$PATH:$ORACLE_HOME/bin:$JAVA_HOME/bin
# No need to input password again everFinally, turn on bash command completion
/usr/bin/keychain $HOME/.ssh/id_rsa
source$HOME/.keychain/$HOSTNAME-sh
source /etc/bash_completio
#2: Setting up bash command prompt
Set custom bash prompt (PS1):PS1='{\u@\h:\w }\$'
#3: Setting default file permissions
## Set default to 644 ##
umask022
#4: Control your shell history settings
# Dont put duplicate lines in the history
HISTCONTROL=ignoreboth
# Ignore these commands
HISTIGNORE="reboot:shutdown *:ls:pwd:exit:mount:man *:history"
# Set history length via HISTSIZE and HISTFILESIZE
exportHISTSIZE=10000
exportHISTFILESIZE=10000
# Add timestamp to historyfile.
exportHISTTIMEFORMAT="%F %T "
#Append to history, don't overwrite
shopt -s histappend
#5: Set the time zone for your session
## set to IST for my own session ##
TZ=Asia/Kolkata
#6: Setting up shell line editing interface
## use a vi-style line editing interface for bash from default emacs mode ##
set -o vi
#7: Setting up your favorite aliases
## add protection ##Here are some additional OS X Unix bash shell aliases:
aliasrm='rm -i'
aliascp='cp -i'
aliasmv='mv -i'
## Memcached ##
aliasmcdstats='/usr/bin/memcached-tool 10.10.29.68:11211 stats'
aliasmcdshow='/usr/bin/memcached-tool 10.10.29.68:11211 display'
aliasmcdflush='echo "flush_all" | nc 10.10.29.68 11211'
## Default command options ##
aliasvi='vim'
aliasgrep='grep --color=auto'
aliasegrep='egrep --color=auto'
aliasfgrep='fgrep --color=auto'
aliasbc='bc -l'
aliaswget='wget -c'
aliaschown='chown --preserve-root'
aliaschmod='chmod --preserve-root'
aliaschgrp='chgrp --preserve-root'
aliasrm='rm -I --preserve-root'
aliasln='ln -i'
# Open desktop apps from bash
aliaspreview="open -a '$PREVIEW'"
aliassafari="open -a safari"
aliasfirefox="open -a firefox"
aliaschrome="open -a google\ chrome"
aliasf='open -a Finder '
# Get rid of those .DS_Store files
aliasdsclean='find . -type f -name .DS_Store -delete'
#8: Colour my world
# Get colored grep output
aliasgrep='grep --color=auto'
exportGREP_COLOR='1;33'
# colored ls too
exportLSCOLORS='Gxfxcxdxdxegedabagacad'
# Gnu/linux ls
ls='ls --color=auto'
# BSD/os x ls command
# aliasls='ls -G'
#9: Setting up your favorite bash functions
# Show top 10historycommand on screen
function ht {
history | awk'{a[$2]++}END{for(i in a){print a[i] "" i}}' | sort -rn | head
}
# Wrapper for host and ping command
# Accept http:// or https:// or ftps:// names for domain and hostnames
_getdomainnameonly(){
localh="$1"
localf="${h,,}"
# remove protocol part of hostname
f="${f#http://}"
f="${f#https://}"
f="${f#ftp://}"
f="${f#scp://}"
f="${f#scp://}"
f="${f#sftp://}"
# remove username and/or username:password part of hostname
f="${f#*:*@}"
f="${f#*@}"
# remove all /foo/xyz.html*
f=${f%%/*}
# show domain name only
echo"$f"
}
ping(){
localarray=( $@ )# get all args in an array
locallen=${#array[@]} # find the length of an array
localhost=${array[$len-1]}# get the last arg
localargs=${array[@]:0:$len-1}# get all args before the last arg in $@ in an array
local_ping="/bin/ping"
localc=$(_getdomainnameonly "$host")
["$t" != "$c"]&& echo"Sending ICMP ECHO_REQUEST to \"$c\"..."
# pass args and host
$_ping$args$c
}
host(){
localarray=( $@ )
locallen=${#array[@]}
localhost=${array[$len-1]}
localargs=${array[@]:0:$len-1}
local_host="/usr/bin/host"
localc=$(_getdomainnameonly "$host")
["$t" != "$c"]&& echo"Performing DNS lookups for \"$c\"..."
$_host$args$c
}
#10: Configure bash shell behavior via shell shopt options command
Finally, you can make changes to your bash shell environment using set and shopt commands:# Correct dir spellings
shopt -q -s cdspell
# Make sure display get updated when terminal window get resized
shopt -q -s checkwinsize
# Turn on the extended pattern matching features
shopt -q -s extglob
# Append rather than overwrite history on exit
shopt -s histappend
# Make multi-line commandsline in history
shopt -q -s cmdhist
# Get immediate notification of background job termination
set -o notify
# Disable [CTRL-D]which is used to exit the shell
set -o ignoreeof
Conclusion
This post is by no means comprehensive. It provided a short walkthrough of how to customize your enviorment. For a thorough look at bash/ksh/zsh/csh/tcsh capabilities, I suggest you read the man page by typing the following command:man bash
manzsh
man tcsh
man ksh
This article was contributed by Aadrika T. J.; Editing and additional content added by admin. You can too contribute to nixCraft.