this post was submitted on 18 Jul 2024
272 points (98.6% liked)

Programmer Humor

19480 readers
1799 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
272
Which one??? (programming.dev)
submitted 3 months ago* (last edited 3 months ago) by [email protected] to c/[email protected]
 

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
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 73 points 3 months ago (4 children)

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.

[–] [email protected] 50 points 3 months ago (2 children)

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

[–] [email protected] 35 points 3 months ago (1 children)

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.

[–] [email protected] 3 points 3 months ago

I was referring more to the graphical representation that the actual flows.

[–] [email protected] 20 points 3 months ago

Agreed! It's cathartic to see it laid out so terrifying. Now I feel less bad for never understanding it.

[–] [email protected] 9 points 3 months ago (1 children)

What do the differently coloured arrows mean? I'm confused.

[–] [email protected] 11 points 3 months ago* (last edited 3 months ago) (1 children)

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.

[–] [email protected] 3 points 3 months ago (1 children)

And what's confusing is that many times those files still manually call the others to make it more logical like zsh. That's what I remember at least, it's been quite a while since I used bash.

[–] [email protected] 4 points 3 months ago

manually call the others

Yeah, most distros will set up source chains to make things nicer for users.

[–] [email protected] 4 points 3 months ago* (last edited 3 months ago) (1 children)

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?

[–] [email protected] 2 points 3 months ago

A lot of systems will call various inits from others to give you a more simplified flow. (Closer to what zsh does.) Check out some of those files in /etc/ it is calling as well as ~/.bash_profile. Or, you might not even be in a login shell!

[–] [email protected] 3 points 3 months ago

Here's the seemingly original source for those who like to know.

[–] [email protected] 35 points 3 months ago* (last edited 3 months ago) (1 children)

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.

[–] [email protected] 2 points 3 months ago (2 children)

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.

[–] [email protected] 2 points 3 months ago* (last edited 3 months ago) (2 children)

All of the repos for my GitHub sourced vim plugins live under one parent directory. I symlink to them from ~/.vim

One example is a simple function that pushes the top level repo directory onto my dir stack and then runs a loop where it pushes each subdir into the stack, runs "ggpull" then pops back to the top level repo directory. ggpull is an alias added by the zsh git plugin. After all repos have been updated it pops back to my original pwd.

I run this as part of my "update all the things" script but sometimes I also want to run it in demand from the cli. So I want this function in all scopes and I want it to have access to "ggpull" in all of those scopes.

[–] [email protected] 2 points 3 months ago

I also "misuse" timewarrior a bit and use it to time things like "how much time do I spend waiting for salt to run". That has its own timewarrior db and a wrapper function for pointing the command at said db. I use this in both login and non login shell contexts.

load more comments (1 replies)
[–] [email protected] 1 points 3 months ago

zshenv’s selling point isn’t necessarily that your typical functions are available across scripts (though that can be neat, too – I source aliasrc as well as an utils script file in my shell config) – it’s that it’s there for non-interactive shells too, whereas zprofile is only applied for login shells (and zshrc only for interactive ones).

So for example, I could open a command in my editor of choice (Helix’s :sh for me), and if I define stuff using the zshenv, all of my aliases etc. are right there. I just have to avoid naming conflicts for script function names if it’s the default shell, but that’s pretty easily done.

[–] [email protected] 31 points 3 months ago (4 children)

Switching to Fish was the best decision I ever made in my terminal. Besides using tmux.

[–] [email protected] 9 points 3 months ago (2 children)

Isn't the POSIX incompatibility a major roadblock when scripting?

[–] [email protected] 18 points 3 months ago

Not really, you can make .fish scripts or call "bash script.sh" when it's more appropriate to be posix compliant

[–] [email protected] 1 points 3 months ago

Not really. I hate writing bash scripts anyway so I usually script whatever I need in Ruby which is far better at scripting than any posix script ever will be, and even when I do need to run a bash script I still can or just shell down into bash or zsh if I really really need to. But that’s like once a year.

[–] [email protected] 8 points 3 months ago* (last edited 3 months ago) (1 children)

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.

[–] [email protected] 8 points 3 months ago (1 children)

The only thing I miss in fish is $?

I know $status exists but my fingers don’t.

[–] [email protected] 2 points 3 months ago

Could you make an alias for it?

[–] [email protected] 4 points 3 months ago (2 children)

Alright stranger, let’s hear it. What is it about Fish that you love so much?

I’ve been generally happy with bash or zsh, pretty much whatever is installed by default (and I honestly don’t know the difference between the two I just mentioned 😬).

[–] [email protected] 5 points 3 months ago

Once upon a time I was the person that was constantly messing with my shell. I would performance tune it, try out new plugins weekly, change my config often. It honestly became a huge waste of time. Since switching to fish I literally haven’t spent any time configuring it at all, besides adding in the fzf plugin and the fzf-marks plugin. It just works. And it has all of the same features that everyone adds to zsh with plugins straight out of the box.

If you’re happy with the default in bash or zsh then you will be even more happy with the default in fish, as it’s just much much more user friendly (which is why so many people add so many plugins to bash and zsh: to make it more user friendly). You can even configure it (even the colors) with a web interface! No mucking about in text files if you don’t want to.

[–] [email protected] 5 points 3 months ago

Zsh has more features and customization options than bash. Fish has a ton of convenience features at the cost of POSIX compatibility (which many view as a good thing)

[–] [email protected] 1 points 3 months ago (3 children)

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.

[–] [email protected] 2 points 3 months ago

I was the person who used every kind of zsh package manager that existed. I even had performance tweaks for some because they were too slow for me. I didn’t even use oh my zsh because it was too slow, and I had hundreds of customizations and maybe 30 plugins.

I no longer needed any of the plugins except fzf and fzf-marks when I switched to fish. It really did simplify my life a whole lot. I don’t ever mess with configs anymore.

load more comments (2 replies)
[–] [email protected] 24 points 3 months ago (1 children)

pretty sure that's your dating profile. And/or cat photos.

[–] [email protected] 22 points 3 months ago

cat ~/Photos/cat.jpeg >> ~/.dating_profile

[–] [email protected] 16 points 3 months ago (1 children)

Hey, I'm a cool kid, too! I have a custom .nanorc

[–] [email protected] 6 points 3 months ago
[–] [email protected] 14 points 3 months ago (1 children)

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

[–] [email protected] 1 points 3 months ago (1 children)

But what would I want in a login shell that I don't also want in a non login shell?

[–] [email protected] 2 points 3 months ago (1 children)

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

load more comments (1 replies)
[–] [email protected] 13 points 3 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.

[–] [email protected] 10 points 3 months ago (1 children)

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.

[–] [email protected] 1 points 3 months ago

What would I want in login environments I wouldn't also want in interactive ones though?

[–] [email protected] 7 points 3 months ago (1 children)

I just thow shit wherever it works lol

[–] [email protected] 18 points 3 months ago* (last edited 3 months ago) (1 children)

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.
[–] [email protected] 10 points 3 months ago (1 children)

We all know what tarot were gettin

[–] [email protected] 2 points 3 months ago (1 children)
[–] [email protected] 6 points 3 months ago

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

[–] [email protected] 1 points 3 months ago* (last edited 3 months ago) (1 children)
[–] [email protected] 1 points 3 months ago

You're aware of what the * does? And of people using other shells?

load more comments
view more: next ›