Welcome to Productivity Friday. This series will post on a Friday and, at least for the time being, will be every other Friday. In the series I will be diving into tips and tricks within my workflow. While mainly technical I’m hoping the series is broad enough to be interesting to developers and system administrators.
Bashing Bash
So this week we will be looking at my bashrc file…
Tim, you use ZSH, we know that, you are a Mac-using coffee-sipping hipster. Yep, you got me on servers I fully control and my own machine, I do indeed use zsh however for work I’m often running on servers where zsh is not available and in those scenarios I have a minimal bash script and a few programs I will place in my own home directory to keep me going.
I have a very tiny build script that takes my dotfiles from version control and a couple of directories and tars the files up, and uploads them to a web-accessible URL.
On a server I wish to add my bash profile to I simply make sure I’m in my home direct, wget/curl the tar file and unpack.
The result means, on the whole, I share a similar Bash and ZSH feeling, particularly when it comes to aliases.
What does my Home directory look like?
/bin/
/logs/
/scripts/
.bashrc
.bash_profile
.bashrc.d/
.bash_autopletion.d/
.ssh/
Bashrc or bash_profile?
So remember back to the post about Nanrc and naming conventions and all that good stuff? Yes. well think of them more as guidelines and actually nothing works as you expect. In most servers if you are using bash you will find two files .bashrc and .bash_profile and if you add content in one or the other it both works.
So you add your aliases in .bash_profile it works fine…
Ah no, you see it does work fine, when you access the terminal via your login. However if you have a separate session that didn’t occur at login then bash_profile is not run for that session.
I see the confusion let me give you an example:
I ssh into example.com, I “login” and get a tty my bash_profile is loaded. HAPPY DAYS
I then run the “screen” command and a new session is created within screen but I didn’t login so bash_profile didn’t run!
But bashrc did run…
But it didn’t run at login, except chances are, on your system, it did.
Yep, it ran on my system, why?
If we open your .bash_profile I’m willing to bet unless you have been editing it that you have something like:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Which is basically, go run .bashrc
Assuming this is the case we can put things in our bashrc file.
Hi MacOS users still using bash
Yeah, so Mac is a little less posix friendly, or rather terminal is. As a consequence, in terminal on a mac, .bash_profile always runs meaning, well, having bashrc becomes less of a thing. To be honest, for pure simplicity, I recommend just using bashrc anyway and adding the code from above.
Bashrc File
My bashrc file is split into sections and these are stored as .bashfiles in .basrc.d directory
aliases/
commands/
misc/
My bashrc file primary looks like:
# Aliases for file in ~/.bashrc.d/aliases}\007"; ':"$PROMPT_COMMAND";
fi
source ~/.iterm2_shell_integration.bash
The final bit is integration with iterm2 to allow coroutines and some basic information to be passed to the terminal.
All that’s left is to add the path (normally set in .bash_profile rather then .bashrc but included here for sake of completeness)
# ---------------------
# Environment variables
# ---------------------
PATH=$PATH:$HOME/bin
export TERM=xterm-256color
export EDITOR=nano
export VISUAL=nano
export PATH
And that’s it my bashrc and bash_profile
Customising BashRC
My BashRC files and bashrc.d is really pretty minimal, I have been toying with making additional changes to simplify it, one of which is to replace the bashrc.d folder and instead run a build script to compile down files I need into a single bashrc I already have this to a certain extent with my in container drop in files. My thinking is then I would just have a small API endpoint and my curl brings in the aliases/commands/ it needs something like
/bashrc/?aliases=centos6,openvz&command=mark,vipe,systemd
which generates a single basrc file.
Want to learn more?
This post is from a series called Productivity Fridays, here is the series so far:
- NanoRC File deep dive
- Minimal Atom
- Bashing my BashRC
Help others find this post:
This post was written by Me, Tim Nash I write and talk about WordPress, Security & Performance.
If you enjoyed it, please do share it!