History
Access history
- There are different ways to repeat commands from history
- The 
up arrow keyallows you to scroll backwards through history Ctrl-rperforms a reverse search based on the pattern entered!nnrepeats a command from history based on its number- !a repeats the last command that starts with letter a
 
 - The 
 
Increase history
To increase the size of your Bash history (the number of commands saved), you need to adjust the HISTSIZE and HISTFILESIZE environment variables. Here’s how to do it
2. Permanent Change
To make the changes permanent, update your Bash configuration file (e.g., ~/.bashrc or ~/.bash_profile):
- Add or modify the following lines:
 
# Increase history size
HISTSIZE=10000       # Number of commands to save in memory
HISTFILESIZE=20000   # Number of commands to save in ~/.bash_history file- source the file
 
source ~/.bashrcsave and delete history
history -w synchronizes the current history from memory to
history file
history -c clears current history, don’t forget to also use history
-w when you want to remove everything
history -d nn removes line nn from current history
Shortcuts
ctrl+a
ctrl+e
ctrl+u
alt+f
alt+b
Shell Expansions

Globbing

Escaping
- In expansion, special characters are interpreted by the shell to attribute a special meaning to the variables
 - In escaping, this special meaning is taken away
- Double quotes suppress 
globbingandshell expansionbut do allowcommandandvariablesubstitution - Single quotes take away the special meaning of 
any characters - Backslash protects the 
following characteronly fromexpansion 
 - Double quotes suppress 
 
Environment Variables
PATHis also an environment varialble
Difference between exporting variables and not exporting

- if you didn’t export the variable only available for 
current shell - if you export the variable only available for 
current terminal session - if you want to make it available for all the sessions then you have to put it on 
~/.bashrc 
alias
- 
`aliasa can be used to define custom commands
 - 
Some commands defined as an alias are provided as system default
 - 
Type alias for an overview of all current aliasses
 - 
Use alias key=value to define your own alias
 
- alias del=‘rm -rf /’
 
- Use 
unaliasto remove an alias 

Control flow
Difference between various bash files
- 
A login shell is initiated when you log in to the system (e.g., through SSH, a terminal emulator, or a graphical login screen).
 - 
A non-login shell is initiated when you open a new terminal in an already logged-in session or execute a script in a new Bash process.
 - 
/etc/profileis the generic Bash startup file containing all system settings that are processed in a login shell (when user authenticates) - 
/etc/profile: Executed only for login shells. This means it runs when a user logs in directly or through SSH, making it suitable for setting up global configurations that should be applied at the start of a user session.when user authenticates
 - 
don’t change
/etc/profiledirectly. instead create a*.shfile in/etc/profile.d/directory - 
/etc/bashrcis processed while opening a subshell - 
/etc/bashrc: Executed for interactive non-login shells. This allows it to be sourced whenever a new terminal window is opened or when running scripts that invoke Bash interactively. It is often referenced in user-specific
.bashrcfiles to include system-wide settings. - 
~/.bash_profileis the user-specific version of/etc/profile - 
~/.bashrcis the user-specific verion of/etc/bashrc 
1. Login Shell Execution Order
A login shell is initiated when you log in to the system (e.g., through SSH, a terminal emulator, or a graphical login screen).
Execution Order:
/etc/profile: The global initialization file executed for all users. It can source additional files like/etc/bashrc.~/.bash_profile: The user’s personal initialization file for login shells. If this file doesn’t exist, Bash tries to read~/.bash_login. If that doesn’t exist either, it reads~/.profile.~/.bashrc: Often sourced from~/.bash_profileto apply both login and interactive settings.
Key Points:
- If 
~/.bash_profilecontains a line likesource ~/.bashrcor. ~/.bashrc, then~/.bashrcwill also be executed during a login shell. /etc/bashrcis not directly invoked in login shells but may be sourced by/etc/profileor~/.bash_profile.
2. Non-Login Shell Execution Order
A non-login shell is initiated when you open a new terminal in an already logged-in session or execute a script in a new Bash process.
Execution Order:
~/.bashrc: This is the primary initialization file for non-login interactive shells./etc/bashrc: Sourced within~/.bashrcor other scripts if configured.
Key Points:
- Non-login shells do not read 
/etc/profileor~/.bash_profile. /etc/bashrcis executed only if explicitly sourced by~/.bashrc.
Summary Table
| File | Login Shell Execution | Non-Login Shell Execution | 
|---|---|---|
/etc/profile | ✔ Yes | ✘ No | 
~/.bash_profile | ✔ Yes | ✘ No | 
/etc/bashrc | ✘ No (unless sourced) | ✘ No (unless sourced) | 
~/.bashrc | ✔ Only if sourced | ✔ Yes | 
Common Practice
- To ensure that settings in 
~/.bashrcapply to login shells, it’s common to add this to~/.bash_profile:if [ -f ~/etc/bashrc ]; then . ~/etc/bashrc fi 

This ensures that both login and non-login shells share the same configuration for interactive commands.