1

I've been creating a project for managing my complete terminal environment. One of the things I manage are the dotFiles. So I have separate dotfiles for e.g.

  • ivonet-alias.sh
  • ivonet-docker.sh
  • ivonet-functions.sh
  • etc.

When I run setup.sh from my project I install symlinks (ln) to my ivonet-*.sh files in my HOME folder. Now to activate them I do the following (once):

prefix="ivonet"
resource="${HOME}/.zshrc"
echo "" >>${resource}
echo "# Personal dotFile resources:" >>${resource}
echo "for f in \${HOME}/.${prefix}-*.sh; do source \${f}; done" >>${resource}

On my own machine I use oh-my-zsh shell and I make it so that that file is changed so that when I open a new terminal session all my dotfiles are sourced in. This works fine but now a couple of colleagues asked to use my setup for dotfiles too and I gave them my repo and it didn't work as I had hoped. The were not using zsh but bash, but even that was not uniform.

When I changed the setup to write to .bashrc it worked for one colleague but not for the other. So in the end I had to do it multiple ways.

  • once in .profile
  • twice in .bash_profile
  • once in .bashrc

Can anyone help me on how I can determine where to make the changes?

I would like to be able to check this from a script something like:

check=$(which bash)
if [[ -z "${check}" ]]; then
   #do stuff here
fi

but I have no idea how to do this. I know it looks like this whats-the-difference-between-bashrc-bash-profile-and-environment but I'm more interested on how I can automatically determine which file is loaded.

Ivonet
  • 2,492
  • 2
  • 15
  • 28
  • Sorry but my question was two fold. The bigger part of my question was on how I can determine it (from a script) – Ivonet Nov 27 '18 at 09:07
  • 1
    As the duplicate explains, it depends on what you want to accomplish. Are these settings for login shells, interactive shells, or neither? Generally `.profile` should be used for stuff which needs to be loaded regardless of which shell the user is using (but of course won't work for shells which are not `sh`-compatible, such as notably the infamous C shell family). You might need to tweak things if the shell doesn't explicitly or implicitly load `.profile` in the scenario you want to support. – tripleee Nov 27 '18 at 09:09
  • you could try `bash -x` – georgexsh Nov 27 '18 at 09:09
  • 1
    I think it is - in general - a bad idea to have a script append something to one of the dot-files in your home. In your case, this is even worse, because you can't know in advance, WHICH dot files will be automatically sourced on WHICH occasion and for WHICH user. Instead, provide your setup in some file where everyone has read-access, and ask the users to source this file from whichever dot-file they want. – user1934428 Nov 27 '18 at 09:10
  • The precise order depends on the contents of some dot files for some shells on some architectures. – tripleee Nov 27 '18 at 09:10
  • @user1934428 I agree but I'm not doing it for a global community but a closed one where we want this as a team. we review every change before it is merged. – Ivonet Nov 27 '18 at 09:17
  • Still a good idea to put the bulk in a separate file and then possibly hook it in from more than one place if necessary. – tripleee Nov 27 '18 at 09:27
  • That is exactly what I'm doing. Making dotfiles separately and hook them in in 1 place... the startup script... – Ivonet Nov 27 '18 at 09:29
  • @Ivonet : It's not so much about the *contents*of the files, but that you do not know **which** shell people use, and how. Some might use a non-login shell, others a login shell; plus, you want to support bash and zsh. Plus, some user (like me) might have set up their dot files so that they can work with both bash and zsh easily, so settings which are common to both, go into a still separate file where you have no way to know the name. Hence you can't know in advance which file you would have to update. – user1934428 Nov 28 '18 at 07:40
  • @user1934428 Again I agree but here it is about how and not if it should be done. Also these are managed pc’s with a limited set of options. My question is not about the ethics but on the how of it. – Ivonet Nov 28 '18 at 07:52
  • @Ivonet: I never talked about ethics, but about technical feasibility. If you really want to modify the user's setup files by a script, you have IMO two options: Either you impose certain rules how each user has to run his shells (and hence you know which files to modify), or you obtain from each user a list of dot-files which he wants to have modified, and you build a static table mapping the user's home directory to the respective file list. – user1934428 Nov 28 '18 at 08:08
  • @user1934428 thanks I’m starting to figure that out :-) – Ivonet Nov 28 '18 at 08:32
  • using the `$SHELL` variable, one can create a switch-case which checks against each of the potential directory-names that `$SHELL` may echo. Upon matching a directory-name, returns the respective profile for that particular directory. For example, if `$SHELL` echoes `/bin/zsh`, then return `~/.zshrc`. If `$SHELL` echoes `/bin/bash`, then return `~/.bashrc`, etc. – Leon the Logician Dec 09 '21 at 17:16

0 Answers0