Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Linux - Shell: Fundamental

Back


What is the Shell


Types of Shells


Find Shell Path

cat /etc/shells
# /bin/sh
# /bin/bash
# /usr/bin/sh
# /usr/bin/bash
echo $SHELL
# /bin/bash

echo $0
# -bash
cat /etc/passwd
# rheladmin:x:1000:1000:rheladmin:/home/rheladmin:/bin/bash

Shell Prompt


Shell prompt customization

Sysmbol Desc
\h Short hostname, up to the first period
\H Full hostname
\n Newline
\d Date in “Weekday Month Date” format “Tue May 26”
\t Current time in 24-hour HH:MM:SS format
\A Current time in 24-hour HH:MM format
\T Current time in 12-hour HH:MM:SS format
\@ Current time in 12-hour am/pm format
\u Username of the current user
\w Current working directory
\W Basename of the current working directory
\$ if the effective UID is 0, a #, otherwise a $
PS1="[\A \u@\h \W] \$ "

Persist PS1 Changes

echo 'export PS1="[\A \u@\h \W]\$ "' >> ~/.bash_profile

Environmental Variables


Environment Variables


Persisting Environment Variables

echo 'export TZ="US/Pacific"' >> ~/.bash_profile

Common Env Var

Environment Variables Variable Description
EDITOR The program to run to perform edits.
HOME The Home directory of the user.
LOGNAME The login name of the user.
MAIL The location of the user’s local inbox.
OLDPWD The previous working directory.
PATH A colon separated list of directories to search for commands.
PAGER This program may be called to view a file.
PS1 The primary prompt string.
PWD The present working directory.
USER The username of the user.

Env Var Management

Command Desc
env list all env var
printenv print all environment
printenv \| less print all environment
printenv ENV_VAR1 ENV_VAR2 print environment var, without $
echo $ENV_VAR1 $ENV_VAR2 print environment var
export ENV_VAR="value" Create/Update an environment var
unset ENV_VAR Removing an env var

Lab: Env

echo $PATH
# /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

printenv PATH
# /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

echo $SHELL
# /bin/bash

printenv SHELL
# /bin/bash

Lab: Create and export Local variable; Unset the variable

# define local
VR1=RHEL9
# display
echo $VR1
# RHEL9

# create a sub-shell
bash
# try to display local var\
# note: nothing returns
echo $VR1
#

# exit the subshell
exit
# make a var an env var
echo $VR1
# RHEL9
export VR1

# create sub-shell
bash
# display env
echo $VR1
# RHEL9
unset VR1
# try to display
# note: nothing returns
echo $VR1


Customize the primary shell prompt

# display the default prompt
echo $PS1
# [\u@\h \W]\$

# modify temporarily
export PS1="<$LOGNAME on $(hostname) in \$PWD>"
# new prompt
# <root on ServerB in /var/log>

# permanently
# Edit the .profile file
vi /root/.bash_profile
# export PS1="<$LOGNAME on $(hostname) in $PWD>"

# logout and login
exit
su -
# <root on ServerB in /root>

Executing Commands


Privileged Execution


Command Path


Lab: which, whereis, type

which cd
# /usr/bin/cd

type cd
# cd is a shell builtin

whereis cd
# cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

man

Shortcut Desc
Enter Move down one line.
Space Move down one page.
g Move to the top of the page.
G Move to the bottom of the page.
q Quit.
man -k key_word

Aliases


Commands

Command Desc
alias List all aliases
alias name='value' Create an alias
unalias name Remove an alias
unalias -a Remove all aliases
alias cls='clear'

Persisting Aliases

echo 'alias cls='clear'' >> ~/.bash_profile

Shell History


history

echo $HISTFILE
# /root/.bash_history

echo $HISTSIZE
# 1000

echo $HISTFILESIZE
# 1000
CMD DESC
history Displays the shell history
history
# 1  clear
# 2  cat /etc/hostname
# 3  clear
# 4  vi /etc/hosts
# 5  clear
# 6  useradd xanadu
# ...

history 5
# 362  echo $HISTSIZE
# 363  echo $HISTFILESIZE
# 364  history
# 365  history --help
# 366  history 5

# To re-execute
# note: #3=clear
!3

# re-execute the most recent occurrence starting with "ch"
# note: chage
!ch

# To remove entry 24 from history:
history -d 24
set +o history
# reenble
set -o history

!syntax

Syntax DESC
!N Repeat command line number N
!! Repeat the previous command line
!string Repeat the most recent command line starting with “string”
head file1 file2 hamlet.txt
# output

!!
# repeat the last command and output

vi !:2
# vi file2
# ! is to repeat the most recent command, which is !!, which is head command
# :N represents the 2nd argument, which is file2.
# so the this command is to open the file2 in vim editor.

head file1 file2 hamlet.txt
vi !$

Grep

CMD DESC
grep root /etc/passwd Search for pattern
grep ^root /etc/passwd Search for pattern at the beginning of the line
grep bash$ /etc/passwd Search for pattern at the end of the line
grep 'aliases and functions' ~/.bashrc Search for pattern
grep -n 'nologin' /etc/passwd show the line numbers
grep -v root /etc/passwd Search exclusively
grep -v $^ ~/.bashrc exclude all the empty lines
grep -i path /etc/bashrc case-insensitive search
grep -w acce.. /etc/lvm/lvm.conf begin with letters “acce” followed by exactly two characters
ls -l /etc \| grep -E 'cron\|ly' Using Regex
grep -ve ^& -ve ^# /etc/ssh/sshd_config Multiple patterns