Axa Assurance Maroc - Insurer Innovation Award 2024
Bash Shell Introduction By Arun Bagul
1. *** BASH (Bourne Again SHell) ***
1) What is shell ?
Shell is tool/utility by which USER interacts with OS (kernel). Shell read (get) commands from
keyboard (entered by user on comand line)
Shell is the commandline INTERPRETER that executes (run) commands.
Linux OS
========================================================
# #
# | ls | | | #
# | pwd |<>| BASH | <> | KERNEL | #
# | ifconfig | | | #
# || #
# linux commands #
#=======================================================
2) How to get list of SHELLs
/etc/shells is a text file which contains the full pathnames of valid login shells on your linux
system!
root@mylaptop:~# cat /etc/shells
# /etc/shells: valid login shells
/bin/csh
/bin/sh
/bin/ksh
/bin/tcsh
/bin/bash
/bin/rbash
...
root@mylaptop:~#
sh Standard shell available on linux
ksh Korn shell
csh C shell
tcsh Turbo Cshell
bash Bourne Again Shell (BASH)
rbash Restricted bash shell
*** Shortcut for bash command line
ctrl+c intrupt command execution (stop command)
ctrl+l clear screen OR clear command
ctrl+d close/exit from shell OR exit command
ctrl+a go to begining of command line
ctrl+e go to end of command line
ctrl+w delete/erase one previous word
altr+d delete/erase next one word
ctrl+u delete/erase text from the current cursor position to the begining of the line
2. ctrl+k delete/erase text from the current cursor position to the end of the line
ctrl+y undo changes
3) Pipe and Output redirection
* Pipe A pipe is a way to connect the output of one program to the input of another program
without any temporary file
root@mylaptop:/var# ls | wc l
root@mylaptop:/var# ls | sort
root@mylaptop:/var# pwd | ls l
* Redirections Before a command is executed, its input and output may be redirected using a
special notation interpreted by the shell. Redirection may also be used to open and close files for the
current shell execution environment. Redirections are processed in the order they appear, from left
to right.
Device File Descriptor
standard input 0
standard output 1
standard error 2
root@mylaptop:~# ls l /dev/stdin
lrwxrwxrwx 1 root root 15 20081005 12:42 /dev/stdin > /proc/self/fd/0
root@mylaptop:~# ls l /dev/stdout
lrwxrwxrwx 1 root root 15 20081005 12:42 /dev/stdout > /proc/self/fd/1
root@mylaptop:~# ls l /dev/stderr
lrwxrwxrwx 1 root root 15 20081005 12:42 /dev/stderr > /proc/self/fd/2
root@mylaptop:~#
** redirects both standard output (file descriptor 1) and standard error (file descriptor 2) to the file
dirlist
# ls > dirlist 2>&1
** redirects only the standard output to file dirlist, because the standard error was duplicated as
standard output before the standard output was
redirected to dirlist.
# ls 2>&1 > dirlist
root@mylaptop:~# ls l > /tmp/mylist ( redirect only standard output to file '/tmp/mylist' )
root@mylaptop:~# ls lMM > /tmp/mylist
ls: invalid option M
Try `ls help' for more information.
root@mylaptop:~# ls lMM 2> /tmp/mylist ( redirect only standard error to file '/tmp/mylist' )
root@mylaptop:~#
** Appending Redirected Output
root@mylaptop:~# ls l >> /tmp/mylist
root@mylaptop:~#
4. ORACLE_BASE=/u01/app
SHELL=/bin/bash
USERNAME=arun
LOGNAME=root
HOME=/root
SESSION_MANAGER=local/mylaptop.com:/tmp/.ICEunix/6670
MAIL=/var/mail/root
DESKTOP_SESSION=default
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/root
...
root@mylaptop:~#
** env command gives list of all system and user defined variables
root@mylaptop:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
root@mylaptop:~#
root@mylaptop:~# echo $PWD
/root
root@mylaptop:~# echo $USER
root
root@mylaptop:~#
HOME The current user's home directory; the default for the cd builtin command. The value of
this variable is also used by tilde (~) expansion
IFS (Internal Field Separater) A list of characters that separate fields; used when the shell splits
words as part of expansion.
MAIL If this parameter is set to a filename and the MAILPATH variable is not set, Bash informs
to user of arrival of mail in the specified file.
PATH A colonseparated list of directories in which the shell looks for commands.
PS1 The primary prompt string. The default value is ‘sv$ ’.
PS2 The secondary prompt string. The default value is ‘> ’.
HOSTNAME The name of the current host.
PPID The process ID of the shell's parent process.
B) User defined variables Created and maintained by user. This type of variable defined in lower
LETTERS.
root@mylaptop:~# num1=10
root@mylaptop:~# num2=30
root@mylaptop:~# expr $num1 + $num2
40
root@mylaptop:~# echo $num1
10
root@mylaptop:~# echo $num2
30
root@mylaptop:~#