So I've been using Bash on various Unices for the longest time - I think I started with BSD back in the late 90s, and then switched to various distributions of Linux, via AIX, and now mainly working with macOS ( which is kinda BSD / Darwin ) and Ubuntu.
Anyway, that's the scene setting out of the way ...
I've got a whole load of aliases set up in my Mac's shell, via ~/.bash_profile - examples include: -
alias hist='history | cut -c 8-'
alias coverage='go test ./... -coverprofile coverage.out && go tool cover -html=coverage.out'
So I've been writing a Bash script to query my IBM Cloud account and list out the various resources that I've created, including OpenShift Container Platform (OCP) clusters, Virtual Private Clouds (VPC), subnets, security groups etc.
And I wanted this script to use my alias for the ibmcloud command-line interface (CLI) tool, namely ic ...
As an example: -
but, when I ran this, I saw: -
./foo.sh: line 3: ic: command not found
I changed my script to dump out my aliases: -
echo "My aliases are : "
which returned: -
My aliases are :
./foo.sh: line 6: ic: command not found
Thankfully, Google came to my aid ...
So today I learned ...
1. There's a difference between .bash_profile and .bashrc
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
So, really, I should be putting my aliases in .bashrc rather than .bash_profile ...
Or duplicating them #yuck
2. I can choose to source .bash_profile from .bashrc meaning that non-interactive shells will pick it up
3. If I add: -
#!/usr/bin/env bash -I
shopt -s expand_aliases