If you need your BASHRC to always contain certain enteries, but they always get cleared upon reboot or upgrade, then this is for you. First move all of your enteries to a file called SCRIPT1. Then make a script called add-SCRIPT1. Then ask cron to launch SCRIPT1 at @reboot (boot up).

EXAMPLE: ReadyNAS OS6. Use at your own Risk. Anytime you update the firmware on ReadyNAS it will clear BASHRC (but not unless you do this)

Imagine a bashrc thats changes with every reboot or firmware update. Perhaps you update the firmware/version on the device and the bashrc (or any other file) for that matter goes back to defaul – so you lose all of your precious bashrc settings.

First off, keep all of your settings in 1 file – this settings file I call SCRIPT1, and I “source”/”run” it from bashrc. The only settings its called /root/scripts/SCRIPT1. This /root/scripts/SCRIPT1 contains everything I put in bashrc (tweaks to get shell colors and better shell history). so I dont just put them in bashrc, I put them in /root/scripts/SCRIPT1 and then I “source /root/scripts/SCRIPT1” from bashrc.

Then to make sure its persistent with reboots & firmware upgrades, I find some application that I know will run everytime, such as cron (luckily in my system, this persists reboots and firmware updates). So I tell cron to add “source /root/scripts/SCRIPT1” to bashrc file upon every boot up, unless that entry is there already.

*** Here is my setup: ***

Type “crontab -e” and add this to your cron setup at the very bottom (use “crontab -l” afterwards to review):
# crontab -l

@reboot /root/scripts/add-SCRIPT1

Then make this add-SCRIPT1 script. This script will add “source SCRIPT1” to bashrc upon bootup only if bashrc doesnt have that line already.

# cat /root/scripts/add-SCRIPT1

if grep -q "^source.*SCRIPT1$" /root/.bashrc; then
echo "/root/.bashrc already contains 'source SCRIPT1'. No need to run this script.";
else
echo "Appending 'source SCRIPT1' to /root/.bashrc";
echo "source /root/scripts/SCRIPT1" >> /root/.bashrc;
fi

Now all you need is your SCRIPT1 file. Here is mine for example (it gives shell history and give shell color):

# cat /root/scripts/SCRIPT1

# shell history (shellhistory)
shopt -s histappend
HISTFILESIZE=1000000
HISTSIZE=1000000
HISTCONTROL=ignoredups
HISTTIMEFORMAT='%F %T '
shopt -s cmdhist
PROMPT_COMMAND='history -a'
# shell colors
export LS_OPTIONS='--color=auto'
export GREP_OPTIONS='--color'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
alias grep='grep $GREP_OPTIONS'
alias egrep='egrep $GREP_OPTIONS'

NOTE: notice that alias used single quotes, so that the $*_OPTIONS variables didnt expand here, but expanded when called upon by the user using the alias commands. So that means one can change *_OPTIONS in the shell and then the alias would take in the new meaning. Its neat and smart and dynamic. So that means you can turn off LS or GREP colors by doing thisĀ export LS_OPTIONS=” or LS_OPTIONS=”. Then you can turn them back on with export LS_OPTIONS=’–color=auto’, or without export prefix. The export prefix simply makes the variable available for subshells (any command ran with ./command1 or bash /command1 creates a new subshell – where it makes its new systen variables & only exported variables are copied to the subshell. On the flipside we can use source ./command1 or . ./command1 which launches the command as part of the main shell sharing the variables).

Leave a Reply

Your email address will not be published. Required fields are marked *