All technological notes.
shell
Bash (Bourne Again Shell)
sh (Bourne shell): Basic and simple.zsh: Advanced features, customizable.csh and tcsh: C-like syntax.ksh (KornShell): Powerful scripting capabilities.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
Format: [user_name@instance_name current_path] $
#:
$:
~:
~jason: /home/jason~root: /root~ftp: /var/ftp, the home dir of ftp service account$PS1.Csh, tcsh, and zsh use $prompt.
PS1| 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] \$ "
echo 'export PS1="[\A \u@\h \W]\$ "' >> ~/.bash_profile
Environmental Variables
echo $VAR_NAMEtypes of variables
Environment Variable
NAME=valueValue:
vs program/process
process is started it inherits the exported environment variables of the process that spawned it.local variables.export command allows variables to be used by subsequently executed commandsecho 'export TZ="US/Pacific"' >> ~/.bash_profile
| 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. |
| 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 |
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
# 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
$PS1: store primary command 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>
$PATH determines command search path.$PATH.
/path/to/command_name./commandroot:
#Normal user:
$sudo: execute a command as another userPath:
search path
echo $PATHwhich:
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
| 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'
.bash_profileecho 'alias cls='clear'' >> ~/.bash_profile
Shell history
~/.bash_history~/.history~/.histfilehistory.bash_history: in the user’s home directory
HISTSIZE: Controls the number of commands to retain in history
1000echo $HISTFILE
# /root/.bash_history
echo $HISTSIZE
# 1000
echo $HISTFILESIZE
# 1000
Searching history:
Ctrl-r: reverse shell history searchArrows: Change the commandEnter: execute the commandCtrl-g: cancel shell history search| 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” |
!:N <Event> <Separator> <Word>
!: the most recent command line!=!!:N: represens a word on the command line.
0 = command1 = first argument!^: the 1st argument!$: the last argument
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 !$
| 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 |