History
Access history
- There are different ways to repeat commands from history
- The
up arrow key
allows you to scroll backwards through history Ctrl-r
performs a reverse search based on the pattern entered!nn
repeats 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 ~/.bashrc
save 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
globbing
andshell expansion
but do allowcommand
andvariable
substitution
- Single quotes take away the special meaning of
any characters
- Backslash protects the
following character
only fromexpansion
- Double quotes suppress
Environment Variables
PATH
is 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
unalias
to 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/profile
is 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/profile
directly. instead create a*.sh
file in/etc/profile.d/
directory -
/etc/bashrc
is 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
.bashrc
files to include system-wide settings. -
~/.bash_profile
is the user-specific version of/etc/profile
-
~/.bashrc
is 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_profile
to apply both login and interactive settings.
Key Points:
- If
~/.bash_profile
contains a line likesource ~/.bashrc
or. ~/.bashrc
, then~/.bashrc
will also be executed during a login shell. /etc/bashrc
is not directly invoked in login shells but may be sourced by/etc/profile
or~/.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~/.bashrc
or other scripts if configured.
Key Points:
- Non-login shells do not read
/etc/profile
or~/.bash_profile
. /etc/bashrc
is 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
~/.bashrc
apply 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.