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.
Type any one of the following command at the Terminal app:
ps $$
ps -p $$
OR
echo "$0"
Sample outputs:
[![Fig.01: Finding out your shell name](http://s0.cyberciti.org/uploads/cms/2015/01/finding-your-shell-like-a-pro.jpg)][1]
Fig.01: Finding out your shell name
### Finding out installed shells ###
To find out the full path for installed shell type:
type -a zsh
type -a ksh
type -a sh
type -a bash
Sample outputs:
[![Fig.02: Finding out your shell path](http://s0.cyberciti.org/uploads/cms/2015/01/finding-and-verifying-shell-path.jpg)][2]
Fig.02: Finding out your shell path
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][3] 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:
Here is a table of commonly used bash shell variables:
![Fig.04: Common bash environment variables](http://s0.cyberciti.org/uploads/cms/2015/01/common-shell-vars.jpg)
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
### Displays the values of environment variables ###
Use any one of the following command to show the values of environment variable called HOME:
## Use printenv ##
printenv HOME
## or use echo ##
echo "$HOME"
# or use printf for portability ##
printf "%s\n" "$HOME"
Sample outputs:
/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 ##
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 export command to set the search path for commands ##
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][6] recommend the following command:
strings =zsh | grep zshrc
Sample outputs:
/etc/zshrc
.zshrc
Type the following command to list your zsh shell files, enter:
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 ###
1. See ~/.profile or /etc/profile file.
### Finding your tcsh shell configuration files ###
1. See ~/.login, ~/.cshrc for the C shell.
2. 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 ls
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
Type history 20 to see the last 20 commands from your history:
history 20
Sample outputs:
[![Fig.06: View session history in the bash shell using history command](http://s0.cyberciti.org/uploads/cms/2015/01/history-outputs.jpg)][7]
Fig.06: View session history in the bash shell using history command
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/bar
!!
To 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 userName
## To log in as a tom user ##
su tom
## To start a new login shell for tom user ##
su tom
## To login as root user ##
su -
## The sudo command syntax (must be configured on your system) ##
sudo -s
sudo tom
See "[Linux Run Command As Another User][8]" post for more on sudo, su and runuser commands.
### 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:
alias c='clear'
alias down='sudo /sbin/shutdown -h now'
Type c alias for the system command clear, so we can type c instead of clear command to clear the screen:
c
Or type down to shutdown the Linux based server:
down
You can create as many aliases you want. See "[30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X][9]" for practical usage of aliases on Unix-like system.
### 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:
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; }
See "[how to write and use shell functions][10]" for more information.
### 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 ####
Finally, you can [make changes to your bash shell environment using set and shopt][14] 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
man zsh
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][15].