The .bashrc file is a list of commands that the bash shell runs when it opens for typed commands without a new login. It lives in your home directory. The leading dot makes it hidden, so use ls -a to see it. People keep short command names, the prompt style and history settings in .bashrc. All of this is useful on any VPS that runs bash.
The examples were checked on December 1, 2026 on Ubuntu 24.04.5 with bash 5.2.21. We ran them as a regular user with a temporary home directory that held copies of the default files. In the output, the machine name is replaced with server and the user name with user.
Which files bash reads
Two terms first. A login shell starts when you log in to the system. An interactive shell waits for you to type commands. These are separate properties, and they can be combined in any way.
There are four personal files:
~/.bashrcis for an interactive shell that is not a login shell;~/.bash_profileis for a login shell;~/.bash_loginis a fallback for the same case;~/.profileis one more fallback, andshunderstands it too.
The ~ sign means your home directory. A login shell first reads the shared /etc/profile. Then it looks for personal files in this order: .bash_profile, .bash_login, .profile. It runs only the first one it finds that it can read. This is described in the GNU Bash manual.
On our system, /etc/skel, the directory with templates for new users, contained .profile and .bashrc out of these files. And .profile loads .bashrc on its own:
if [ -n "$BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fiThe first line checks that the shell is bash. The second checks that the file exists. A dot followed by a space runs the file in the current shell. The fi lines close the conditions. That is why settings from .bashrc are visible after login as well.
Checking with markers
We added echo commands with markers to the first line of .profile and to the first and last lines of .bashrc, for example echo "MARK .bashrc (top)". Then we started bash with different options. The -c option runs a command from a string, -l makes the shell a login shell, and -i makes it interactive. The command inside prints the shell properties: $- shows the enabled modes, and shopt -q login_shell tells whether it was a login. The < /dev/null tail feeds empty input; we explain why below.
bash -lc 'echo "flags=$-"; shopt -q login_shell && echo login || echo non-login' < /dev/nullMARK .profile
MARK .bashrc (top)
flags=hBc
loginThe same run with -ic:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
MARK .bashrc (top)
MARK .bashrc (bottom)
flags=hiBHc
non-loginThe first two lines appeared because there was no terminal. The i letter in flags means interactive mode.
| Command | Markers that fired |
|---|---|
bash -c | none |
bash -lc | .profile, top of .bashrc |
bash -ic | top and bottom of .bashrc |
bash -lic | .profile, top and bottom of .bashrc |
The second row has no bottom of .bashrc. This is not a mistake; the next section explains it.
Next we created .bash_login with a single marker. On login, only that file fired. Then we added .bash_profile, and now only that one ran. .profile and .bashrc stayed silent even with -lic. If your settings disappeared after such a file showed up, add this line to it:
[ -f ~/.bashrc ] && . ~/.bashrcThe [ -f ~/.bashrc ] part checks whether the file exists. The && operator runs the right side only if the left side succeeds. With this line, .bashrc was read again. It does not bring back the rest of .profile, though.
The early return: why a line at the end does not work
Near the top of the default .bashrc on our test system (lines 5-9) there is this block:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esacThe first line is a comment. case $- in looks at the shell modes. If i is among them, nothing happens and the file is read further. In all other cases return stops reading. esac closes the construct.
We appended export APP_ENV=production to the end of the file. The export command creates a variable that child programs receive too. APP_ENV is just an example here.
bash -icshowedAPP_ENV=[production];bash -lcshowedAPP_ENV=[];bash -c 'source ~/.bashrc; echo "APP_ENV=[$APP_ENV]"'also showedAPP_ENV=[].
The last case matters for scripts. Sourcing the file explicitly does not get around the check.
What happens with a command over SSH
A command like ssh server command does not open an interactive session. Yet bash can read .bashrc in this case too: it tries to detect that it was started over the network. We did not make a real SSH connection. Instead we set the SSH_CLIENT variable with a documentation address, so this is only an emulation. The env -u SHLVL part removes the shell nesting counter: without it the marker will not appear in a normal session.
env -u SHLVL SSH_CLIENT='203.0.113.10 50000 22' bash -c 'echo cmd-ran' < /dev/nullMARK .bashrc (top)
cmd-ranThe top of the file ran, the bottom did not. The variable from the end of the file stayed empty. When we moved the export above the check, it appeared.
There is a subtlety. When the counter is set (SHLVL=1), the marker disappears. Also, a plain bash -c read .bashrc when its input was a socket: bash took it for a start over the network. That is why the checks above use < /dev/null.
The conclusion is simple. Put above the check only what remote commands really need. Those lines must not print anything, or the extra text ends up in the reply of the remote command, and they must not terminate the shell. For your own scripts, set variables inside the scripts. Keep aliases in mind too: bash does not expand them in scripts by default. We declared one inside a script and got hi: command not found.
Back up before editing
cp ~/.bashrc ~/.bashrc.bak-$(date +%F)cp copies the file. $(date +%F) inserts the date, so we got the name .bashrc.bak-2026-12-01. Running it again on the same day overwrites the copy. If the file is already broken by then, the good copy is lost. So make the copy before the first edit.
Now open the file, for example with nano ~/.bashrc, and append this block to the end:
# --- my settings ---
alias ports='ss -tuln'
case ":$PATH:" in
*":$HOME/scripts:"*) ;;
*) export PATH="$HOME/scripts:$PATH" ;;
esac
export EDITOR=nano
PS1='[\A] \u@\h:\w\$ '
HISTSIZE=5000
HISTFILESIZE=10000
HISTCONTROL=ignoreboth:erasedupsThe first line is a comment that makes your own settings easy to find. Let's go through the rest.
Alias, variable and prompt
alias ports='ss -tuln' creates an alias, which is a short name. Now you can type ports instead of the longer command. ss -tuln itself lists listening TCP and UDP ports: -t and -u select these protocols, -l keeps listening sockets, and -n shows numbers instead of names. On the test system we only checked that the alias was created and did not run the command. How to read its output and open the port you need is covered in checking open ports on a Linux server. Do not put spaces around the = sign.
export EDITOR=nano sets an environment variable. Many programs look at it when they choose a text editor.
We checked how it differs from a plain variable:
A=plain; export B=exported
bash -c 'echo "A=[$A] B=[$B]"' < /dev/nullA=[] B=[exported]The first line creates two variables, and the second one reads them from a child shell. Only the exported variable reached it.
PS1 defines how the prompt looks. Here \A is the time as hours:minutes, \u is the user, \h is the short machine name, and \w is the current directory. \$ shows # for root and $ for everyone else. Square brackets, @ and the colon are printed as they are. The result looked like this:
[12:26] user@server:~$For a colored prompt, wrap the color codes in \[ and \]. This tells bash that these characters take no space on screen. The default .bashrc has an example.
PATH without duplicates
PATH is a colon-separated list of directories. Bash searches them for programs. People often write this:
export PATH="$HOME/scripts:$PATH"The line puts your scripts directory in front of the old list. Create the directory with mkdir -p ~/scripts; the -p option avoids an error if it already exists.
At first we used exactly this simple line and ran the file three times in the current shell with source ~/.bashrc (more on it below). Then we split PATH into lines: tr replaces colons with line breaks.
echo "$PATH" | tr ":" "\n"/home/user/scripts
/home/user/scripts
/home/user/scripts
/usr/local/bin
/usr/bin
/binThree copies. After exec bash there were four: the new shell received the old PATH and added the directory once more.
That is why our block has a check. case ":$PATH:" in takes the list with colons at both ends so that whole directory names are compared. The line with $HOME/scripts and an empty action means the path is already there, so nothing happens. The *) branch adds it when there was no match. ;; ends a branch, and esac ends the whole check. In a new shell, after sourcing three times, we counted the copies:
echo "$PATH" | tr ":" "\n" | grep -c "/scripts$"grep -c counts matching lines, and /scripts$ looks for that ending. The answer is 1. This check does not remove copies that have already piled up.
And do not lose the old list. We wrote export PATH="$HOME/scripts" without :$PATH, and ls answered ls: command not found. With the full path /usr/bin/ls it worked. In our session, export PATH=/usr/local/bin:/usr/bin:/bin helped: it brought back the main directories. This is a temporary measure; the line in the file still has to be fixed.
The default .profile also adds ~/bin and ~/.local/bin by itself if they exist. We checked this with ~/bin: it shows up in PATH when a login shell starts.
Command history: HISTSIZE and HISTCONTROL
HISTSIZE=5000is how many commands to keep in the history list;HISTFILESIZE=10000is how many lines to keep in the history file;HISTCONTROL=ignoreboth:erasedupsis what to filter out.
ignoreboth combines two rules: do not save commands that start with a space, and do not save a repeat of the previous command. erasedups removes earlier identical lines from the list before saving. The colon separates the values.
We cleared the list with history -c. Do not repeat this on a server if you need that list. Then we printed the settings and typed echo one, echo two, echo one twice, and one command with a leading space. history showed:
1 echo "$HISTSIZE $HISTFILESIZE $HISTCONTROL"
2 echo two
3 echo one
4 historyThe repeats are gone, and so is the command with the space. After HISTSIZE=3, only the last three lines remained. Do not treat the leading space as protection for passwords: it only keeps the command out of bash history.
The default file on our system had 1000, 2000 and ignoreboth. We appended our block to the end, so it overrides them.
source bashrc and exec bash: how to reload settings
Check the syntax first:
bash -n ~/.bashrc; echo "rc=$?"The -n option reads commands without running them. $? is the exit code of the previous command. We got rc=0. After that, apply the file:
source ~/.bashrcsource runs the file in the current shell. A dot does the same: . ~/.bashrc. Before this command we had no ports alias, after it the alias appeared.
Another way to reload .bashrc:
exec bashexec replaces the current shell with a new program. No new process is created.
The difference shows when you remove settings. We deleted the alias ports and export EDITOR lines from the file. After source, both values stayed in memory. Before exec bash we also set a variable MYVAR without export and changed to /tmp.
After exec bash | Result |
|---|---|
alias ports | gone |
exported variable EDITOR | still there: nano |
variable MYVAR without export | gone |
process ID and SHLVL | unchanged |
| current directory | still /tmp |
So exec bash does not give you a clean environment. unset NAME removes an unwanted variable, and unalias name removes an alias. Or log out and log in again.
One more detail: exec bash turned a login shell into a non-login shell. exec bash -l made it a login shell again.
Errors in .bashrc and getting access back
It is often said that a syntax error in .bashrc locks you out. We tried two variants and got a different result.
First we left an if without the closing fi. bash -n returned code 2:
/home/user/.bashrc: line 123: syntax error: unexpected end of fileThe file had 122 lines: bash reached the end and did not find fi. The line number shows where bash noticed the problem, not where you made the mistake. Both bash and bash -l printed the error, but the shell kept working. The alias defined before the broken block was created, the one after it was not.
Then we put a stray ) on the second line. The shell started again. Nothing below the parenthesis ran, including the default ll alias.
This does not mean that every error is harmless. Here are three lines that did get in the way on our system. bash -n returned 0 for each of them.
| Line at the end of the file | What happened |
|---|---|
exit 1 | bash -l exited right away with code 1 |
set -e | the shell worked but closed after ls /nonexistent |
exec bash | the shell restarted endlessly and ran no commands |
exit 1 terminates the shell. set -e tells it to exit when a command fails; this rule has exceptions. In the third test we added a counter next to exec bash: it reached 284 starts in about 5 seconds.
How to recover
The main rule: after an edit, do not close the current session. Open a second connection and check that you can log in there. If it fails, fix the file from the first one.
Restore the copy, using the date from your own file name:
cp ~/.bashrc.bak-2026-12-01 ~/.bashrc
bash -n ~/.bashrcThe first command replaces the broken file with the copy. The second checks the syntax: if everything is fine, it prints nothing.
From a session that is already open, you can start bash without personal settings:
bash --noprofile --norc--noprofile skips the login files, and --norc skips .bashrc. With exit 1 at the end of the file, we got a working bash-5.2$ prompt.
If you have no open session and the bad line is below the check, a remote command may help. Replace user and the documentation address with your own:
ssh user@203.0.113.10 'mv ~/.bashrc ~/.bashrc.broken'ssh runs the quoted command on the server, and mv renames the broken file. After that you can log in and restore the copy. In the emulation with exit 1, renaming and restoring worked. We did not test this over real SSH.
If exit 1 is above the check, things are worse. In the emulation, the remote command did not run. A nested bash --noprofile --norc did not help either: the outer shell read .bashrc and exited first. Then you need another way to reach the files that does not start this shell: another account with enough rights, or a recovery mode if there is one. Logging in as the same user through a console also reads .bashrc with the default .profile, so we expect it will not help. We did not test another account, recovery mode or the console.
root, regular users and system-wide settings
Every user has their own .bashrc; for root it usually lives in /root, and your personal settings do not carry over to it. System-wide settings for login shells go into /etc/profile, and your own additions go into separate *.sh files in /etc/profile.d, which /etc/profile on our system reads one by one.
In our test, an interactive non-login shell read /etc/bash.bashrc and the personal .bashrc, but not /etc/profile. We ran nothing as root. We only opened the templates for root files from the base-files system package: the .bashrc there differs from the regular user one. So read the file you actually have before editing it.
What this test does not cover
We did not use containers or a clean Ubuntu image: the checks ran on an installed system with a temporary home directory, so your set of shared files may differ. We only emulated SSH. We did not check a VPS console or recovery mode, work as root or through sudo or su, copying files with scp, sftp and rsync, runs from cron and services, graphical terminals, other shells or other distributions.
We also did not run unset, unalias or ss -tuln, did not edit the file in nano, did not create our own files in /etc/profile.d, and did not test history from several sessions or trimming by HISTFILESIZE.
Short checklist
- Back up
.bashrc. - Add personal conveniences at the end, below the check.
- When adding a directory to
PATH, keep the old list and check for duplicates. - Run
bash -n ~/.bashrc. Remember that it checks syntax, not the meaning of commands. - Keep the current session open and test login in a second one.
- If something goes wrong, restore the copy.



