Fuck it, .zshrc it is.

Image transcription:

  • Top text: I STILL DON’T KNOW WHAT SHOULD GO IN .*RC VERSUS .*PROFILE
  • Bottom text: AND AT THIS POINT I’M AFRAID TO ASK
  • bigredgiraffe@lemmy.world
    link
    fedilink
    arrow-up
    49
    arrow-down
    1
    ·
    2 months ago

    I found this diagram on SO at one point but I can’t find the post and it is the best explanation I have found for how all of the files work for bash and zsh, each color is an individual path of execution (eg, follow the red line).

    Bottom line though, it only really matters if you are overriding something that is already defined, for example I tell my users to use zshrc and I provide defaults and common things in zprofile because zshrc is executed last when they login.

    • JackbyDev@programming.devOP
      link
      fedilink
      English
      arrow-up
      34
      ·
      2 months ago

      I feel like I couldn’t make this more confusing if I tried. What is doing on with the golden arrows around /etc/profile??

      • AggressivelyPassive@feddit.de
        link
        fedilink
        arrow-up
        26
        ·
        2 months ago

        That’s decades of legacy for you…

        I bet each step/arrow/decision had a good reason at some point, but most of them probably back when computers lived in caves and hunted their tapes using spears and rocks.

        I feel like we’re slowly reaching a point where the complexity is collapsing in on itself - just look at the absolute chaos a modern web app is.

      • Gamma@programming.dev
        link
        fedilink
        English
        arrow-up
        4
        ·
        edit-2
        2 months ago

        Select the color which matches the steps before filenames ((non-)login and (non-)interactive), then follow that arrow the rest of the way. There’s more colors in Bash because Bash makes a distinction between remote and local shells.

        Another way to look at the same data for Zsh (note: $ZDOTDIR will be used instead of $HOME if it’s defined at any step along the way):

        File neither interactive login both
        /etc/zshenv x x x x
        ${ZDOTDIR:-$HOME}/.zshenv x x x x
        ${ZDOTDIR:-$HOME}/.zprofile x x
        ${ZDOTDIR:-$HOME}/.zshrc x x
        ${ZDOTDIR:-$HOME}/.zlogin x x
        ${ZDOTDIR:-$HOME}/.zlogout x x

        One confusion on the Bash side of the diagram is that you see branching paths into ~/.profile, ~/.bash_profile and ~/.bash_login. Bash will use for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and execute only the first one that exists and is readable.

    • mumblerfish@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      2 months ago

      Is this a diagram for how it should work? Not how it actually works? Like I put my stuff in the ~/.bashrc, mostly because I think the debian one says like “put your fun stuff below here” or something. The green and grey lines go through the ~/.bashrc, but both of them go through the “no login” bubble in the diagram. But I know my ~/.bashrc works, so the diagram is a suggestion?

  • Korthrun@lemmy.sdf.org
    link
    fedilink
    arrow-up
    31
    ·
    edit-2
    2 months ago

    It’s all about context. If you write a convenience function and put it in zshrc, scripts you run from the cli will not have access to the function as defined in zshrc. Same with aliases added by zsh plugins etc.

    If you need “the thing” on the command line, zshrc. If you also need it in scripts you run from the cli, toss it in the profile file.

    ETA: I personally keep the functions I want to access from scripts in .zshenv as I recall reading that this file is ALWAYS sourced.

    • Gamma@programming.dev
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 months ago

      What kind of functions do you write which you share between your scripts? Generally if I’m wanting to reuse a non-trivial function, I extend the functionality of the first script instead.

    • devfuuu@lemmy.world
      link
      fedilink
      arrow-up
      6
      arrow-down
      1
      ·
      edit-2
      2 months ago

      Yeah. Fish just simplifies life everywhere. No longer do I need to care about those silly files or other configurations for basic stuff like search history.

      Best decision ever.

    • JackbyDev@programming.devOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 months ago

      I have sort of been eyeing fish on and off for years. I enjoy my oh my zsh setup and have it somewhat customized. I use a modified version of the funky theme. (I can share if interested.) When I’m at work I don’t try new things that might affect my productivity (like trying a new shell) and when I have motivation to do techy stuff in my free time I really need to utilize it to do what I want because my focus really meanders.

  • Crow@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    11
    ·
    2 months ago

    Afaik the difference is whether a session is interactive or not, and non-interactive ones don’t source the rc

      • Crow@lemmy.blahaj.zone
        link
        fedilink
        arrow-up
        1
        ·
        2 months ago

        Presumably just to get rid of unnecessary stuff that would slow down the start of all bash instances. Also aliases that could fuck with scripts

  • dohpaz42@lemmy.world
    link
    fedilink
    English
    arrow-up
    10
    arrow-down
    1
    ·
    2 months ago

    So, assuming bash is your shell, the ~/.bash_profile is executed for login shells for the user logging in.

    ~/.bashrc is the initialization script for when starting a bash session (for the current user).

    In my experience, the ~/.bashrc will also execute ~/.bash_profile if it exists. So you can use either.

  • Gamma@programming.dev
    link
    fedilink
    English
    arrow-up
    6
    ·
    2 months ago

    When in doubt, ~/.zshrc. It’s the right choice 99% of the time. Otherwise, there’s a chance you fuck up scripts you’ve installed which assume no shell options have been changed in non-interactive contexts.

  • BeigeAgenda@lemmy.ca
    link
    fedilink
    arrow-up
    5
    ·
    2 months ago

    What about what I do: Just add . ~/.bash_BeigeAgenda at the bottom of one of the files, for all my own crap.

    • MajorHavoc@programming.dev
      link
      fedilink
      arrow-up
      15
      ·
      edit-2
      2 months ago

      You’ve shared the true bash configuration flow chart.

      1. Add this line to your .bashrc.
      2. If that didn’t work, move it into .bash_profile.
      3. If that didn’t work, call up that bash fanatic you know, and let them screw with both files for like 15 minutes.
      4. If that didn’t work, there’s this tarot card reader I know that comes very recommended. Maybe we can get a bulk discount.