Skip to main content

Songs on the Security of Networks
a blog by Michał "rysiek" Woźniak

Useful Bash defaults done right

This is an ancient post, published more than 4 years ago.

As such, it might not anymore reflect the views of the author or the state of the world. It is provided as historical record.

I finally got to do some holiday clean-ups on many of (Debian) the servers I administer. And so, I got around to cleaning-up my Bash configs and finding out how everything I wanted done exactly should be done. The Right Way.

What I wanted

My expectations are meager:

  • screen, starting by default with each login, connecting to an already running screen session or starting a new one should that be unavailable;
  • some nice aliases (for me – ls and grep with colour; ll for ls -l);
  • colourful prompt;
  • system-wide default editor set to vim.

This isn’t much, but helps in my day-to-day sysadmin life.

How it’s achieved - for normal users

Starting screen by default
Just add a single line at the very end of /etc/skel/.profile:

screen -xRR

Nice aliases
That’s all in /etc/skel/.bashrc, just uncomment the ones you like.

Colourful prompt
The best way to do this is, again, to edit /etc/skel/.bashrc file and simply un-comment the line:

#force_color_prompt=yes

I also set my custom colourful prompt style

if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[echo "\w" | sed "s/\//\[\e[0;34m\](\e[1;36m\]\u\[\e[0;37m\]@\[\e[0;36m\]\h\[\e[0;37m\]:\[\e[1;34m\]$()\/\[)\[\e[1;37m\](\e[1;34m\]/g")\$\[\e[0m\] '

Vim as the system default editor
That will actually work for all users, including root. Just install vim and (as root) run:

update-alternatives --config editor

Therein, select the editor of choice. Done.

Every single new user on the system will have the above as defaults. However, remember to copy /etc/skel/.profile and /etc/skel/.bashrc to homedirs of any users already present in the system!

Some tweaks for root

The root account does not use /etc/skel/ files as base, so we need to edit /root/.bashrc and put there the aliases we need, etc. Also, using screen auto-startup with root doesn’t seem to be a good idea, so I just pass on that.

As for root user’s colourful prompt, I want it to be nice and red, but without the (superfluous) username. So, here it is:

PS1='${debian_chroot:+($debian_chroot)}\[echo "\w" | sed "s/\//\[\e[0;34m\](\e[0;31m\]\h\[\e[0;33m\]:\[\e[1;34m\]$()\/\[)\[\e[1;31m\](\e[1;34m\]/g")\$\[\e[0m\] '

That’s it.