Know About Terminals of Linux, OSx & Unix

Ref: http://www.cyberciti.biz/

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

Finding out your current shell

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

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
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:

Fig. 03: Bash shell nesting level (subshell numbers)

Changing your current shell permanently with chsh command

Want to change your own shell from bash to zsh permanently? Try:

chsh -s /bin/zsh

Want to change the other user’s shell from bash to ksh permanently? Try:

sudo chsh -s /bin/ksh userNameHere

 

Finding out your current environment

You need to use the

env
env | more
env | less
env | grep 'NAME'

Sample outputs:

TERM_PROGRAM=Apple_Terminal
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

Here is a table of commonly used bash shell variables:

Fig.04: Common bash environment variables

Leave a Reply

Your email address will not be published.