this post was submitted on 18 Jul 2024
272 points (98.6% liked)
Programmer Humor
19488 readers
1012 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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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.
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.
zshenv
’s selling point isn’t necessarily that your typical functions are available across scripts (though that can be neat, too – I sourcealiasrc
as well as anutils
script file in my shell config) – it’s that it’s there for non-interactive shells too, whereaszprofile
is only applied for login shells (andzshrc
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 thezshenv
, 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.