0

I have a file named ~/myScripts/assignments.sh which contains various assignments such as variables and aliases. Here's a taste from that file:

drt="/var/www/html"
rss="/etc/init.d/php*-fpm restart && systemctl restart nginx.service"

alias drt="cd ${drt}"
alias rss="${rss}"

I use these assignments frequently from the moment I finished installing my operating system, especially to write neater scripts for installation, configuration and maintenance of my webserver and adjacent software.

Thus, it's vital that this file will always be exported, that its data will always be available in all Bash sessions, immediately after any Bash session has started (also after a reboot).

To achieve that, I thought of the following lousy script:

source ~/myScripts/assignments.sh # Immediate availability;
printf "\n%s" "source ~/myScripts/assignments.sh" >> ~/.profile
cat > "cron_daily.sh" <<< "source ~/myScripts/assignments.sh"

crontab <<-"CRONTAB"
    0 0 * * * ~/myScripts/cron_daily.sh # Permanent availability (after the one minute gap);
CRONTAB

What will be a good approach to achieve the state I described above?

Update

The reason I'd think to avoid sourcing the file, then add source ~/myScripts/assignments.sh inside bash.bashrc is that I've seen some devops reluctant from sourcing bash.bashrc in general. Although, when the file isn't customized, or has just such small change it is generally not a problem.

7

If the assignments are necessary for "all" bash-sessions, simply put the file in somewhere like /etc/assignments and source it globally from /etc/bash.bashrc.

Append this into /etc/bash.bashrc:

source /etc/assignments

That way, you have all your definitions available in all bash-sessions, for every user, and can maintain the information in a separate file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.