SysAdmin: Shells Part-1
A brief story about linux shells
At its base, a shell is simply a macro processor that executes commands. The term macro processor means functionality where text and symbols are expanded to create larger expressions.
A Unix shell is both a command interpreter and a programming language.
Evolution of shell:
- sh-Bourne shell (introduced by Stephen Bourne in 1977)
- csh-C shell (developed by Bill Joy in 1978)
- ksh-Korn shell (developed by David Korn in 1983)
- bash-Bourne again shell (developed by Brian Fox in 1989)
Z Shell(zsh) is considered most powerful shell.
In order to add a specific shell in linux, first install the shell and then type chsh
followed by location of the shell.
Order of Execution
- In-built command
- Alias of a command
- Command in the directory mentioned in $PATH
- If the command is not found it’ll show “Command not found”
Terminal
According to opensource.com, the Linux terminal is a text-based interface used to control a Linux computer. It’s just one of the many tools provided to Linux users for accomplishing any given task, but it’s widely considered the most efficient method available.
Shell tricks
Important shell commands
pushd
— push directory location in the stack and set pwd
to stack’s topmost directory location
dirs
— shows all the locations in the stack
popd
— pop directory location from the stack and set pwd
to stack’s topmost directory location
Important history commands
history
— shows the commands executed previously
!20
— executes the 20th command in history
!-2
— executes the 2nd recent command in history
!*
— appends previous line argument to current line
!!
— appends previous line to current line
Ctrl+r
— reverse index search
History configuration commands
HISTCONTROL="ignorespace"
— not add the commands starting with space to history
HISTCONTROL="ignoredups"
— not add the commands to history which are already present
HISTCONTROL="erasedups"
— erases the duplicate commands in history when any specific command is executed
HISTCONTROL="erasedups:ignorespace"
— erases duplicate as well as ignores command starting with space
HISTIGNORE="history*:ls*"
— ignores history and ls commands
HISTTIMEFORMAT="%d %b %H:%M:%S -> "
— shows timestamp for every command in history
HISTSIZE="10000"
— set history size i.e., number of history command. For infinite set to “-1”. For turning off the history recording set to “0”.
HISTFILESIZE="10000"
— set history file size (location:/home/<username>/.bash_history).
Everytime the system is rebooted:
- The history file is overwritten. In order to append the history to previous session’s history use shell option
shopt -s histappend
- All the configuration is reset to default, in order to persist the changes copy the output of
set | grep ^HIST*
to .bash_profile in home directory of user and also append “export ” before all HIST-commands.