* Re: Best practices for managing aliases in ohmyzsh?
2021-08-30 23:22 ` Steve Dondley
@ 2021-08-30 23:34 ` Ray Andrews
2021-08-31 1:44 ` Steve Dondley
2021-08-31 0:14 ` Lawrence Velázquez
2021-08-31 8:26 ` Roman Perepelitsa
2 siblings, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-08-30 23:34 UTC (permalink / raw)
To: zsh-users
On 2021-08-30 4:22 p.m., Steve Dondley wrote:
>
> Yes, I spent about 2 hours today figuring out how to get my old bash
> functions automatically loaded. Previously, I was just using 'source'
> to load the old bash files which, from what I gathered, is not the
> best way with zsh.
That's all I ever do. They're scripts, so you source them. I'm
unfamiliar with bash, but just off hand, I'm wondering what else *but*
source them would even be possible. Except for the automatically
called files of course which will automatically be sourced if they exist.
>
> Now you've got me wondering. While I was bumbling around today, I
> noticed I had an ~/.oh-my-zsh/.zshrc file. I'm only using the ~/.zshrc
> file, though. It has just one line:
> ZSH_THEME="powerlevel9k/powerlevel9k". I'm guessing that's the default
> because I don't remember editing it.
Personally I just hate all the automatic stuff, you have files being
accessed that you might not even know exist. Fine for the adept of
course, but when I was learning zsh I killed all of them just so I could
be sure of what was going on.
>
> So what is the "real" .zszhrc supposed to be?
The docs are pretty readable as to the startup protocols, but at the
very least '.zshrc' should be in your home directory. God knows what
oh-my-zsh does. Never used it, but around here the feeling one gets is
that it's not the fashion -- reputation is that it's too much desert and
not enough dinner if you get me. My own philosophy is to keep things a
simple as possible. I end up saying 'oh my!' enough as it is ;-)
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-30 23:34 ` Ray Andrews
@ 2021-08-31 1:44 ` Steve Dondley
0 siblings, 0 replies; 53+ messages in thread
From: Steve Dondley @ 2021-08-31 1:44 UTC (permalink / raw)
To: zsh-users
> That's all I ever do. They're scripts, so you source them. I'm
> unfamiliar with bash, but just off hand, I'm wondering what else *but*
> source them would even be possible. Except for the automatically
> called files of course which will automatically be sourced if they
> exist.
My understanding is that if you use the autoload feature, the functions
are only loaded on an as-needed basis. That's not something I really
need but I figured it may come in handy as I write a bunch of new
functions. Plus I was just curious about to see how it worked for future
reference.
> Personally I just hate all the automatic stuff, you have files being
> accessed that you might not even know exist. Fine for the adept of
> course, but when I was learning zsh I killed all of them just so I
> could be sure of what was going on.
For me, part of the fun (and frustration) is figuring out what these
more advanced systems can do. On the other hand, it's definitely nice to
know exactly how exactly how a simpler setup works so you aren't trying
to reverse-engineer mysterious behavior.
>> So what is the "real" .zszhrc supposed to be?
> The docs are pretty readable as to the startup protocols, but at the
> very least '.zshrc' should be in your home directory. God knows what
> oh-my-zsh does. Never used it, but around here the feeling one gets
> is that it's not the fashion -- reputation is that it's too much
> desert and not enough dinner if you get me. My own philosophy is to
> keep things a simple as possible. I end up saying 'oh my!' enough as
> it is ;-)
OK, good to know. Wasn't sure if I was out to lunch. And I just reread
the ohmzsh install instructions and it says to place .zshrc in the home
directory so it sounds likes I'm good.
Thanks!
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-30 23:22 ` Steve Dondley
2021-08-30 23:34 ` Ray Andrews
@ 2021-08-31 0:14 ` Lawrence Velázquez
2021-08-31 1:29 ` Steve Dondley
2021-08-31 8:26 ` Roman Perepelitsa
2 siblings, 1 reply; 53+ messages in thread
From: Lawrence Velázquez @ 2021-08-31 0:14 UTC (permalink / raw)
To: Steve Dondley; +Cc: zsh-users
On Mon, Aug 30, 2021, at 7:22 PM, Steve Dondley wrote:
> >> Now I'm wondering if there is some similar mechanism for adding
> >> aliases with ohmyzsh.
Note that oh-my-zsh is a separate project that is not affiliated
with this one. There might be someone on this list who is familiar
with how it works, but it's hardly a guarantee.
> >> If not, what's everyone else doing who has a
> >> couple hundred aliases. Are you just throwing them all into .zshrc or
> >> doing something to help manage all your aliases?
>
> > I'm the least qualified guy on the list, but I can tell you that you
> > can take advantage of all the built in complexities that zsh offers or
> > you can make it as dead simple as you desire.
I don't use any aliases, and only a couple of functions. But, as
Ray said, you can organize things however you like. One rudimentary
method is to just separate your alias definitions into their own
file and source it from $ZDOTDIR/.zshrc:
### $ZDOTDIR/zsh_aliases ###
alias foo='one thing'
alias bar='another thing'
### $ZDOTDIR/.zshrc ###
. $ZDOTDIR/zsh_aliases
Going further, you could use multiple files, organize them in a
directory hierarchy, etc.
> Yes, I spent about 2 hours today figuring out how to get my old bash
> functions automatically loaded. Previously, I was just using 'source' to
> load the old bash files which, from what I gathered, is not the best way
> with zsh.
In general, zsh will not run bash code 100% correctly unless it is
written *extremely* portably. Your old functions almost certainly
need to be tweaked to one degree or another.
> Now you've got me wondering. While I was bumbling around today, I
> noticed I had an ~/.oh-my-zsh/.zshrc file. I'm only using the ~/.zshrc
> file, though. It has just one line:
> ZSH_THEME="powerlevel9k/powerlevel9k". I'm guessing that's the default
> because I don't remember editing it.
I don't use any zsh configuration frameworks, but in my experience
they don't love it when you work with one foot inside their black
box and the other outside of it.
> So what is the "real" .zszhrc supposed to be?
Whatever you want it to be. The distribution usually includes the
"zsh-newuser-install" function, which is intended to interactively
guide you through creating a .zshrc file.
https://zsh.sourceforge.io/Doc/Release/User-Contributions.html#User-Configuration-Functions
--
vq
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 0:14 ` Lawrence Velázquez
@ 2021-08-31 1:29 ` Steve Dondley
0 siblings, 0 replies; 53+ messages in thread
From: Steve Dondley @ 2021-08-31 1:29 UTC (permalink / raw)
To: zsh-users
> Note that oh-my-zsh is a separate project that is not affiliated
> with this one. There might be someone on this list who is familiar
> with how it works, but it's hardly a guarantee.
Right. I had hunted around for an ohmyzsh list but didn't find one (but
if you know of one, let me know). My original question wasn't about
ohmyzsh so sorry for going off on an unrelated tangent.
>> >> If not, what's everyone else doing who has a
>> >> couple hundred aliases. Are you just throwing them all into .zshrc or
>> >> doing something to help manage all your aliases?
>>
>> > I'm the least qualified guy on the list, but I can tell you that you
>> > can take advantage of all the built in complexities that zsh offers or
>> > you can make it as dead simple as you desire.
>
> I don't use any aliases, and only a couple of functions. But, as
> Ray said, you can organize things however you like. One rudimentary
> method is to just separate your alias definitions into their own
> file and source it from $ZDOTDIR/.zshrc:
>
> ### $ZDOTDIR/zsh_aliases ###
> alias foo='one thing'
> alias bar='another thing'
>
> ### $ZDOTDIR/.zshrc ###
> . $ZDOTDIR/zsh_aliases
Yeah, this is the direction I'm leaning.
> Going further, you could use multiple files, organize them in a
> directory hierarchy, etc.
In bash, I had a fancy system for grouping aliases by category into
their own files. It was probably more trouble than it's worth. Probably
easiest to have one big file for aliases and comment off the different
sections.
>
> In general, zsh will not run bash code 100% correctly unless it is
> written *extremely* portably. Your old functions almost certainly
> need to be tweaked to one degree or another.
My functions are very simple so I haven't run into any problems yet. But
I will definitely keep this in mind going forward.
> I don't use any zsh configuration frameworks, but in my experience
> they don't love it when you work with one foot inside their black
> box and the other outside of it.
I mostly installed it to experiment with the different prompts the
built-in themes provided and out of curiosity to see what some of the
advanced capabilities of zsh might be. It's probably not the brightest
idea to learn zsh through the lens of ohmyzsh.
>
>> So what is the "real" .zszhrc supposed to be?
>
> Whatever you want it to be. The distribution usually includes the
> "zsh-newuser-install" function, which is intended to interactively
> guide you through creating a .zshrc file.
>
> https://zsh.sourceforge.io/Doc/Release/User-Contributions.html#User-Configuration-Functions
OK, thanks. I appreciate the time.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-30 23:22 ` Steve Dondley
2021-08-30 23:34 ` Ray Andrews
2021-08-31 0:14 ` Lawrence Velázquez
@ 2021-08-31 8:26 ` Roman Perepelitsa
2021-08-31 15:51 ` Steve Dondley
2 siblings, 1 reply; 53+ messages in thread
From: Roman Perepelitsa @ 2021-08-31 8:26 UTC (permalink / raw)
To: Steve Dondley; +Cc: Ray Andrews, Zsh Users
On Tue, Aug 31, 2021 at 1:23 AM Steve Dondley <s@dondley.com> wrote:
>
> Now you've got me wondering. While I was bumbling around today, I
> noticed I had an ~/.oh-my-zsh/.zshrc file.
This is unexpected. Oh My Zsh installer does not create this file.
> I'm only using the ~/.zshrc file, though. It has just one line:
> ZSH_THEME="powerlevel9k/powerlevel9k". I'm guessing that's the
> default because I don't remember editing it.
The default theme in Oh My Zsh is "robbyrussell". powerlevel9k is not
included in Oh My Zsh. The fact that you are using it means you've
installed it separately (or copied a config from someone else who
did).
Note that development of powerlevel9k has been discontinued and the
project has been archived.
On Tue, Aug 31, 2021 at 3:30 AM Steve Dondley <s@dondley.com> wrote:
>
> Right. I had hunted around for an ohmyzsh list but didn't find one (but
> if you know of one, let me know).
Oh My Zsh uses Discord for questions and discussions of this kind. You
can find a link at https://ohmyz.sh/.
> > In general, zsh will not run bash code 100% correctly unless it is
> > written *extremely* portably. Your old functions almost certainly
> > need to be tweaked to one degree or another.
>
> My functions are very simple so I haven't run into any problems yet. But
> I will definitely keep this in mind going forward.
This suggests that all or at least most of your functions could be
scripts instead. Place each one in a separate executable file in a
directory in your PATH (~/bin is a common choice), slap a shebang on
each and you are good to go. This way you can use your utilities from
any shell and won't have to port them whenever you switch shells.
Since these utilities are proven to work in bash, use `#!/usr/bin/env
bash` as shebang. You *can* also make them auto-loadable functions in
zsh but you probably shouldn't. The only advantage would be that
they'll execute faster but that matters only in rare special cases.
The disadvantage is that you would have to port your scripts to zsh,
make sure they can run with various shell options, don't pollute
interactive shell environment, etc,
Roman.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 8:26 ` Roman Perepelitsa
@ 2021-08-31 15:51 ` Steve Dondley
2021-08-31 17:57 ` Roman Perepelitsa
0 siblings, 1 reply; 53+ messages in thread
From: Steve Dondley @ 2021-08-31 15:51 UTC (permalink / raw)
To: zsh-users
On 2021-08-31 04:26 AM, Roman Perepelitsa wrote:
> On Tue, Aug 31, 2021 at 1:23 AM Steve Dondley <s@dondley.com> wrote:
>>
>> Now you've got me wondering. While I was bumbling around today, I
>> noticed I had an ~/.oh-my-zsh/.zshrc file.
>
> This is unexpected. Oh My Zsh installer does not create this file.
>
>> I'm only using the ~/.zshrc file, though. It has just one line:
>> ZSH_THEME="powerlevel9k/powerlevel9k". I'm guessing that's the
>> default because I don't remember editing it.
>
> The default theme in Oh My Zsh is "robbyrussell". powerlevel9k is not
> included in Oh My Zsh. The fact that you are using it means you've
> installed it separately (or copied a config from someone else who
> did).
>
> Note that development of powerlevel9k has been discontinued and the
> project has been archived.
>
> On Tue, Aug 31, 2021 at 3:30 AM Steve Dondley <s@dondley.com> wrote:
>>
>> Right. I had hunted around for an ohmyzsh list but didn't find one
>> (but
>> if you know of one, let me know).
>
> Oh My Zsh uses Discord for questions and discussions of this kind. You
> can find a link at https://ohmyz.sh/.
>
>> > In general, zsh will not run bash code 100% correctly unless it is
>> > written *extremely* portably. Your old functions almost certainly
>> > need to be tweaked to one degree or another.
>>
>> My functions are very simple so I haven't run into any problems yet.
>> But
>> I will definitely keep this in mind going forward.
>
> This suggests that all or at least most of your functions could be
> scripts instead. Place each one in a separate executable file in a
> directory in your PATH (~/bin is a common choice), slap a shebang on
> each and you are good to go. This way you can use your utilities from
> any shell and won't have to port them whenever you switch shells.
> Since these utilities are proven to work in bash, use `#!/usr/bin/env
> bash` as shebang. You *can* also make them auto-loadable functions in
> zsh but you probably shouldn't. The only advantage would be that
> they'll execute faster but that matters only in rare special cases.
> The disadvantage is that you would have to port your scripts to zsh,
> make sure they can run with various shell options, don't pollute
> interactive shell environment, etc,
>
> Roman.
OK, thanks Roman. This all sounds like good advice has got me rethinking
things.
But I'm a little confused on a point you made. What do you consider the
difference to be between a script and a function? My functions are
typically called with aliases that I've set up. Example:
# usage: taskwarrior_cmd
# description: runs a task warrior command
cmd=${1}
shift
id=${1}
#echo $id;
shift
echo $cmd;
#echo $@
echo task $id ${cmd}$@
task $id ${=cmd}$@
#echo $cmd
if [[ "$cmd" =~ ^done ]]; then
task +st and \(+imm or +OVERDUE\) next
fi
Do you consider this to be a function or a script? I have this in a file
in my ~/.ohmyzsh functions directory. I don't call this function
directly, it is only called from an alias that looks something like
this:
alias tdep="taskwarrior_cmd 'modify depends:'"
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 15:51 ` Steve Dondley
@ 2021-08-31 17:57 ` Roman Perepelitsa
2021-08-31 18:54 ` Bart Schaefer
` (2 more replies)
0 siblings, 3 replies; 53+ messages in thread
From: Roman Perepelitsa @ 2021-08-31 17:57 UTC (permalink / raw)
To: Steve Dondley; +Cc: Zsh Users
On Tue, Aug 31, 2021 at 5:52 PM Steve Dondley <s@dondley.com> wrote:
>
> But I'm a little confused on a point you made. What do you consider the
> difference to be between a script and a function?
By script I mean an executable file, a.k.a. an external command or utility.
If you run `type foo` and it says "foo is a shell function" then it's
a function. If it says `foo is /bin/foo`, then it's an executable
file. Executable files written in interpreted languages are scripts.
Many utilities can be implemented either as functions or as scripts.
Here's an example:
% rm foo
% >foo <<<'echo "I am a script"'
% chmod +x foo
% path+=($PWD)
% function bar() echo "I am a function"
% type foo
foo is /tmp/foo
% type bar
bar is a shell function
% foo
I am a script
% bar
I am a function
All utilities that can be implemented as scripts can also be
implemented as functions but there are utilities that can be
implemented as functions but not as scripts. For example, here's a
function I use often:
function md () {
(( ARGC == 1 )) && mkdir -p -- "$1" && cd -- "$1"
}
Upon executing `md /tmp/foo` I expect my current directory to be
/tmp/foo. This cannot work if md is a script.
So functions can do things that scripts cannot. Another advantage is
that they are faster to execute by 1 millisecond or so. Other than
that, scripts are better. They are much more resilient to changes in
your environment and won't break when you change options in your
interactive shell, define aliases, change shells, etc. Writing
functions that reliably work in interactive shells is not too hard but
it's definitely an advanced skill.
So the rules of thumb are:
1. If it cannot be a script, it has to be a function. Duh.
2. If it can be a script and 1ms overhead doesn't matter, it should be a script.
Always remember to add a shebang to your scripts. Since your scripts
are known to work in bash, start all of them with this line:
#! /usr/bin/env bash
> [snip shell code]
>
> Do you consider this to be a function or a script?
If you put this in an executable file in your PATH, it'll be a script.
I recommend doing that. You can also create a function with that body
but that would be worse.
> I don't call this function directly, it is only called from an alias
Aliases can invoke scripts just as easily as they can invoke functions.
Roman.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 17:57 ` Roman Perepelitsa
@ 2021-08-31 18:54 ` Bart Schaefer
2021-08-31 21:22 ` Steve Dondley
2021-09-01 0:12 ` Ray Andrews
2 siblings, 0 replies; 53+ messages in thread
From: Bart Schaefer @ 2021-08-31 18:54 UTC (permalink / raw)
To: Roman Perepelitsa; +Cc: Steve Dondley, Zsh Users
On Tue, Aug 31, 2021 at 11:01 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> Aliases can invoke scripts just as easily as they can invoke functions.
However, the rules for scripts or functions invoking aliases are
considerably more convoluted.
Scripts can use aliases if the aliases are defined in a startup file
that the script loads, but any script using "zsh -f" will not load any
such files, so in practice scripts usually cannot use aliases.
Functions can use aliases (in general) if the alias is defined before
the function. There's a whole section on this in the FAQ document.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 17:57 ` Roman Perepelitsa
2021-08-31 18:54 ` Bart Schaefer
@ 2021-08-31 21:22 ` Steve Dondley
2021-08-31 21:36 ` Roman Perepelitsa
2021-08-31 21:44 ` Roman Perepelitsa
2021-09-01 0:12 ` Ray Andrews
2 siblings, 2 replies; 53+ messages in thread
From: Steve Dondley @ 2021-08-31 21:22 UTC (permalink / raw)
To: zsh-users
> If you put this in an executable file in your PATH, it'll be a script.
> I recommend doing that. You can also create a function with that body
> but that would be worse.
>
>> I don't call this function directly, it is only called from an alias
>
> Aliases can invoke scripts just as easily as they can invoke functions.
>
> Roman.
So to sum up, it sounds like your advice is "Always use a script instead
of a function. If the script can't work for whatever reason, then use a
function."
I will do this.
To keep things organized, I think I set up a subdirectory called
~/bin/shell_alias_scripts, add it to $PATH and toss all the scripts that
are used by aliases in there. And so long as I only call these scripts
with an alias, there shouldn't be any performance penalty because they
won't all have to be sourced by zsh.
Thanks!
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 21:22 ` Steve Dondley
@ 2021-08-31 21:36 ` Roman Perepelitsa
2021-08-31 21:43 ` Steve Dondley
2021-08-31 21:44 ` Roman Perepelitsa
1 sibling, 1 reply; 53+ messages in thread
From: Roman Perepelitsa @ 2021-08-31 21:36 UTC (permalink / raw)
To: Steve Dondley; +Cc: Zsh Users
On Tue, Aug 31, 2021 at 11:23 PM Steve Dondley <s@dondley.com> wrote:
>
> So to sum up, it sounds like your advice is "Always use a script instead
> of a function. If the script can't work for whatever reason, then use a
> function."
Close enough.
> To keep things organized, I think I set up a subdirectory called
> ~/bin/shell_alias_scripts, add it to $PATH and toss all the scripts that
> are used by aliases in there. And so long as I only call these scripts
> with an alias, there shouldn't be any performance penalty because they
> won't all have to be sourced by zsh.
If you only intend to invoke these scripts via aliases and never
directly from interactive shells or from other scripts, then you don't
need to put them in PATH. You can invoke them via their full names
instead.
alias foo='~/.local/libexec/bar -blah'
~/.local/libexec would be your local equivalent of /usr/libexec, which
contains a bunch of executable files that aren't meant to be in PATH.
If you do intend to invoke these scripts either from interactive
shells or from other scripts, your plan is fine, although I would just
put all these scripts in ~/bin.
Roman.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 21:36 ` Roman Perepelitsa
@ 2021-08-31 21:43 ` Steve Dondley
0 siblings, 0 replies; 53+ messages in thread
From: Steve Dondley @ 2021-08-31 21:43 UTC (permalink / raw)
To: Roman Perepelitsa; +Cc: Zsh Users
> If you only intend to invoke these scripts via aliases and never
> directly from interactive shells or from other scripts, then you don't
> need to put them in PATH. You can invoke them via their full names
> instead.
>
> alias foo='~/.local/libexec/bar -blah'
Yeah, good point. I can put these scripts anywhere. Probably best to use
a variable for the directory path in case I want to change it.
This sounds like a saner plan than what I have started doing. Thanks for
your advice.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 21:22 ` Steve Dondley
2021-08-31 21:36 ` Roman Perepelitsa
@ 2021-08-31 21:44 ` Roman Perepelitsa
1 sibling, 0 replies; 53+ messages in thread
From: Roman Perepelitsa @ 2021-08-31 21:44 UTC (permalink / raw)
To: Steve Dondley; +Cc: Zsh Users
On Tue, Aug 31, 2021 at 11:23 PM Steve Dondley <s@dondley.com> wrote:
>
> And so long as I only call these scripts
> with an alias, there shouldn't be any performance penalty because they
> won't all have to be sourced by zsh.
Forgot to mention that there is no performance difference between
invoking a script directly or via an alias. When you use an alias, it
first gets expanded, and then the expansion is executed as if you
typed it. For example:
% alias foo=cat
% foo
When you hit ENTER after foo, zsh will realize that foo is an alias
and will replace it with cat. After that it'll execute cat the same
way as if you typed it yourself. Aliases are like (unhygienic) macros.
When you execute an executable file -- be it a script or a binary file
-- zsh never sources it. It uses execve(2) or something similar to
execute the file. To source a file you need to explicitly run `source
foo` or `. foo`.
Roman.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-08-31 17:57 ` Roman Perepelitsa
2021-08-31 18:54 ` Bart Schaefer
2021-08-31 21:22 ` Steve Dondley
@ 2021-09-01 0:12 ` Ray Andrews
2021-09-01 0:28 ` Bart Schaefer
2 siblings, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 0:12 UTC (permalink / raw)
To: zsh-users
On 2021-08-31 10:57 a.m., Roman Perepelitsa wrote:
> function md () {
> (( ARGC == 1 )) && mkdir -p -- "$1" && cd -- "$1"
> }
>
> Upon executing `md /tmp/foo` I expect my current directory to be
> /tmp/foo. This cannot work if md is a script.
>
>
contents of script 'test':
(( ARGC == 1 )) && mkdir -p -- "$1" && cd -- "$1"
... giving it a run:
4 /aWorking/Zsh/Source/Wk/Junk 0 $ . test /tmp/foo
4 /tmp/foo 0 $
... seems to work fine, I must be misunderstanding you.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 0:12 ` Ray Andrews
@ 2021-09-01 0:28 ` Bart Schaefer
2021-09-01 2:33 ` Ray Andrews
0 siblings, 1 reply; 53+ messages in thread
From: Bart Schaefer @ 2021-09-01 0:28 UTC (permalink / raw)
To: Ray Andrews; +Cc: Zsh Users
On Tue, Aug 31, 2021 at 5:12 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> ... giving it a run:
No, that's sourcing it. "Giving it a run" would be
4 /aWorking/Zsh/Source/Wk/Junk 0 $ chmod +x ./test
5 /aWorking/Zsh/Source/Wk/Junk 0 $ ./test /tmp/foo
> I must be misunderstanding you.
Sourcing a file is not the same as executing a script.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 0:28 ` Bart Schaefer
@ 2021-09-01 2:33 ` Ray Andrews
2021-09-01 3:01 ` Bart Schaefer
` (2 more replies)
0 siblings, 3 replies; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 2:33 UTC (permalink / raw)
To: zsh-users
On 2021-08-31 5:28 p.m., Bart Schaefer wrote:
> No, that's sourcing it. "Giving it a run" would be
> 4 /aWorking/Zsh/Source/Wk/Junk 0 $ chmod +x ./test
> 5 /aWorking/Zsh/Source/Wk/Junk 0 $ ./test /tmp/foo
>
>> I must be misunderstanding you.
> Sourcing a file is not the same as executing a script.
>
Yikes, I'm missing something fundamental. In my head, executing a
script is identical to sourcing it. It's not a binary, so what else can
there be than interpreting the file? If a function is declared (yes I
know, wrong word but you get my meaning) then the function enters
memory, otherwise it's interpret-and-forget until next time, no? If
'chmod +x' then you don't hafta 'say' 'source me', it's automatic, and
it will be found if on the path, etc, but I'm laboring under the idea
that nothing else is different. Is this going to hurt? I don't
understand the lesson of Roman's example, so I think it is going to hurt.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 2:33 ` Ray Andrews
@ 2021-09-01 3:01 ` Bart Schaefer
2021-09-01 14:20 ` Daniel Shahaf
2021-09-01 15:58 ` Ray Andrews
2021-09-01 3:19 ` Steve Dondley
2021-09-01 3:27 ` Lawrence Velázquez
2 siblings, 2 replies; 53+ messages in thread
From: Bart Schaefer @ 2021-09-01 3:01 UTC (permalink / raw)
To: Ray Andrews; +Cc: Zsh Users
On Tue, Aug 31, 2021 at 7:33 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Yikes, I'm missing something fundamental. In my head, executing a
> script is identical to sourcing it. It's not a binary, so what else can
> there be than interpreting the file?
The file typically names the interpreter that will understand it.
That's the point of the shebang (#!) line at the top of the file.
Thus you can have zsh scripts, bash scripts, perl scripts, python
scripts, etc. and the shell (or some other program) that wants to run
the script doesn't have to be the same one that understands the
scripts. It just says "execute this" and the operating system looks
at the #! and starts up (a new invocation of) the correct program.
That new copy "sources" the file.
The context of the discussion has been (1) how to zsh interactively
but run scripts written for bash, without having to convert them, and
(2) how to make all those scripts accessible to zsh without having to
predefine them somehow in .zshrc. The answer is to make them
executable and put bash in the #! line, so that bash continues to be
what runs them. That works as long as what the script does, does not
need to persist in the interactive zsh (see "new invocation" above).
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 3:01 ` Bart Schaefer
@ 2021-09-01 14:20 ` Daniel Shahaf
2021-09-01 15:58 ` Ray Andrews
1 sibling, 0 replies; 53+ messages in thread
From: Daniel Shahaf @ 2021-09-01 14:20 UTC (permalink / raw)
To: zsh-users
Bart Schaefer wrote on Tue, Aug 31, 2021 at 20:01:18 -0700:
> The context of the discussion has been (1) how to zsh interactively
> but run scripts written for bash, without having to convert them, and
> (2) how to make all those scripts accessible to zsh without having to
> predefine them somehow in .zshrc. The answer is to make them
> executable and put bash in the #! line, so that bash continues to be
> what runs them. That works as long as what the script does, does not
> need to persist in the interactive zsh (see "new invocation" above).
A future FAQ entry about this:
https://github.com/zsh-users/zsh/blob/64befeb4ca5bd9ddb7162584585cd9041711ff8c/Etc/FAQ.yo#L939-L997
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 3:01 ` Bart Schaefer
2021-09-01 14:20 ` Daniel Shahaf
@ 2021-09-01 15:58 ` Ray Andrews
2021-09-01 18:22 ` Lawrence Velázquez
1 sibling, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 15:58 UTC (permalink / raw)
To: zsh-users
On 2021-08-31 8:01 p.m., Bart Schaefer wrote:
>
> The answer is to make them
> executable and put bash in the #! line, so that bash continues to be
> what runs them.
Ok, that's all transparent. What puzzles me is why the now executable
file won't run Roman's example. Just tried it, and indeed it doesn't
work. Lots of test lines work but not the 'mkdir' and not the 'cd'.
'cd' being a builtin I'm wondering why it doesn't work in an executed file:
#!/usr/bin/zsh
echo "This echos just fine"
cd -- "/tmp"
... no 'cd'. So far I've not come across any reason to do anything
other than source my scripts but if I ever do I'd like to understand this.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 15:58 ` Ray Andrews
@ 2021-09-01 18:22 ` Lawrence Velázquez
2021-09-01 19:11 ` Ray Andrews
0 siblings, 1 reply; 53+ messages in thread
From: Lawrence Velázquez @ 2021-09-01 18:22 UTC (permalink / raw)
To: Ray Andrews; +Cc: zsh-users
On Wed, Sep 1, 2021, at 11:58 AM, Ray Andrews wrote:
> Ok, that's all transparent. What puzzles me is why the now executable
> file won't run Roman's example. Just tried it, and indeed it doesn't
> work. Lots of test lines work but not the 'mkdir' and not the 'cd'.
No. Those commands do work. You are either invoking the example
incorrectly or testing the results incorrectly.
> 'cd' being a builtin I'm wondering why it doesn't work in an executed file:
>
> #!/usr/bin/zsh
> echo "This echos just fine"
> cd -- "/tmp"
>
> ... no 'cd'.
https://mywiki.wooledge.org/BashFAQ/060
https://mywiki.wooledge.org/BashGuide/Sourcing
--
vq
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 18:22 ` Lawrence Velázquez
@ 2021-09-01 19:11 ` Ray Andrews
2021-09-01 19:16 ` Bart Schaefer
0 siblings, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 19:11 UTC (permalink / raw)
To: zsh-users
On 2021-09-01 11:22 a.m., Lawrence Velázquez wrote:
> No. Those commands do work. You are either invoking the example
> incorrectly or testing the results incorrectly.
> https://mywiki.wooledge.org/BashFAQ/060
Of course. Like a sourced script, an 'executable' creates it's own
fresh environment. Doh. I really should have figured that out. But
... Roman said it wouldn't work which is why I was primed to find it
didn't work. So there's still a loose thread.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 19:11 ` Ray Andrews
@ 2021-09-01 19:16 ` Bart Schaefer
2021-09-01 19:56 ` Ray Andrews
0 siblings, 1 reply; 53+ messages in thread
From: Bart Schaefer @ 2021-09-01 19:16 UTC (permalink / raw)
To: Ray Andrews; +Cc: Zsh Users
On Wed, Sep 1, 2021 at 12:12 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Of course. Like a sourced script, an 'executable' creates it's own
> fresh environment. Doh. I really should have figured that out.
No, you're still misunderstanding (or making typos). A sourced script
DOES NOT create its own fresh environment, it affects the environment
of the program that sources it.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 19:16 ` Bart Schaefer
@ 2021-09-01 19:56 ` Ray Andrews
2021-09-01 20:03 ` Bart Schaefer
0 siblings, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 19:56 UTC (permalink / raw)
To: zsh-users
On 2021-09-01 12:16 p.m., Bart Schaefer wrote:
> No, you're still misunderstanding (or making typos). A sourced script
> DOES NOT create its own fresh environment, it affects the environment
> of the program that sources it.
>
But ... don't I recall that a script runs in a separate process? Doing
some quick looking (zsh is the same I trust):
Bash scripts are not kept in the shell’s memory. *Whenever a
script is executed, a new subshell (non-interactive shell) is
forked to execute the script.* And within the subshell, the script
is executed by the interpreter specified in the shebang line
<https://www.baeldung.com/linux/shebang> (for example,
/#!/bin/bash/). But, if required, we can also execute the script
within the current shell using the /source/ command.
(think, think, think ... )
Ok, I was too flabby in interpreting 'execute'. I've taken 'source' to
be identical to 'execute'. Not so. But ... what if you have a shebang
in a sourced file? One might suppose that forces a subshell, no?
Everything I write has the shebang, I just put it there every time.
What if you source a file with another shell's shebang? Possible
paradox there. It would *have* to run the other shell in a subshell
unavoidably there -- or just barf.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 19:56 ` Ray Andrews
@ 2021-09-01 20:03 ` Bart Schaefer
2021-09-01 20:52 ` Steve Dondley
` (2 more replies)
0 siblings, 3 replies; 53+ messages in thread
From: Bart Schaefer @ 2021-09-01 20:03 UTC (permalink / raw)
To: Ray Andrews; +Cc: Zsh Users
On Wed, Sep 1, 2021 at 12:56 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Ok, I was too flabby in interpreting 'execute'. I've taken 'source' to
> be identical to 'execute'. Not so. But ... what if you have a shebang
> in a sourced file? One might suppose that forces a subshell, no?
No. "#!" is a comment, because it begins with "#".
> What if you source a file with another shell's shebang?
Sufficiently portably written shell code might work, and there are
some tricks you can do in some languages that have superficial
similarities so that incorrect interpreters can recognize themselves
and do a backflip, but for the most part it is indeed "barf."
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 20:03 ` Bart Schaefer
@ 2021-09-01 20:52 ` Steve Dondley
2021-09-01 21:06 ` Ray Andrews
2021-09-01 20:53 ` Lawrence Velázquez
2021-09-01 21:05 ` Ray Andrews
2 siblings, 1 reply; 53+ messages in thread
From: Steve Dondley @ 2021-09-01 20:52 UTC (permalink / raw)
To: Bart Schaefer; +Cc: Ray Andrews, Zsh Users
> No. "#!" is a comment, because it begins with "#".
Correct. #! is treated as "magic" sequence by the OS. It's only looked
for when you first execute a file. Otherwise it is treated as a command
and ignored.
Here's more information than you could possibly read through:
https://www.in-ulm.de/~mascheck/various/shebang/
>
>> What if you source a file with another shell's shebang?
>
> Sufficiently portably written shell code might work, and there are
> some tricks you can do in some languages that have superficial
> similarities so that incorrect interpreters can recognize themselves
> and do a backflip, but for the most part it is indeed "barf."
The shebang line will get ignored, as you pointed. Any code will be
interpreted as the shell of the parent sourcing the file. If the script
was written in bash but happens to run fine in zsh, it will work
perfectly fine.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 20:52 ` Steve Dondley
@ 2021-09-01 21:06 ` Ray Andrews
2021-09-01 22:06 ` Steve Dondley
0 siblings, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 21:06 UTC (permalink / raw)
To: zsh-users
On 2021-09-01 1:52 p.m., Steve Dondley wrote:
>
>> No. "#!" is a comment, because it begins with "#".
>
> Correct. #! is treated as "magic" sequence by the OS. It's only looked
> for when you first execute a file. Otherwise it is treated as a
> command and ignored.
... treated as a comment, I think you mean?
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 21:06 ` Ray Andrews
@ 2021-09-01 22:06 ` Steve Dondley
0 siblings, 0 replies; 53+ messages in thread
From: Steve Dondley @ 2021-09-01 22:06 UTC (permalink / raw)
To: Ray Andrews; +Cc: zsh-users
On 2021-09-01 05:06 PM, Ray Andrews wrote:
> On 2021-09-01 1:52 p.m., Steve Dondley wrote:
>>
>>> No. "#!" is a comment, because it begins with "#".
>>
>> Correct. #! is treated as "magic" sequence by the OS. It's only looked
>> for when you first execute a file. Otherwise it is treated as a
>> command and ignored.
> ... treated as a comment, I think you mean?
Ugh. Yeah. My brain has a mind of its own I cannot control when I write
and I'm just too damn impatient to proof it. I'll never learn.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 20:03 ` Bart Schaefer
2021-09-01 20:52 ` Steve Dondley
@ 2021-09-01 20:53 ` Lawrence Velázquez
2021-09-01 21:11 ` zeurkous
2021-09-01 21:23 ` Ray Andrews
2021-09-01 21:05 ` Ray Andrews
2 siblings, 2 replies; 53+ messages in thread
From: Lawrence Velázquez @ 2021-09-01 20:53 UTC (permalink / raw)
To: Ray Andrews; +Cc: zsh-users
On Wed, Sep 1, 2021, at 4:03 PM, Bart Schaefer wrote:
> On Wed, Sep 1, 2021 at 12:56 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
> > What if you source a file with another shell's shebang?
Even before getting to language mismatches, it generally doesn't
make sense to source a file that has a shebang because such a file
probably assumes that it'll be run in a separate process. For
instance, it might create a lot of temporary variables or utility
functions and not clean any of it up.
Similarly, I don't think you should include a shebang in a file
that is not meant to be executed by a separate interpreter because
that is the whole point of shebangs. Including one sends a strong
signal that that file is supposed to be executed.
> Sufficiently portably written shell code might work, and there are
> some tricks you can do in some languages that have superficial
> similarities so that incorrect interpreters can recognize themselves
> and do a backflip, but for the most part it is indeed "barf."
Observe:
% cat ./barf.py
#!/usr/bin/python
import sys
print(sys.argv)
% chmod +x ./barf.py
% ./barf.py 1 2 3
['./barf.py', '1', '2', '3']
% . ./barf.py
./barf.py:3: command not found: import
./barf.py:4: unknown file attribute: y
--
vq
^ permalink raw reply [flat|nested] 53+ messages in thread
* RE: Best practices for managing aliases in ohmyzsh?
2021-09-01 20:53 ` Lawrence Velázquez
@ 2021-09-01 21:11 ` zeurkous
2021-09-01 21:19 ` Bart Schaefer
2021-09-01 21:23 ` Ray Andrews
1 sibling, 1 reply; 53+ messages in thread
From: zeurkous @ 2021-09-01 21:11 UTC (permalink / raw)
To: Lawrence Velázquez, Ray Andrews; +Cc: zsh-users
Haai,
"Lawrence Velázquez" <larryv@zsh.org> wrote:
> Similarly, I don't think you should include a shebang in a file
> that is not meant to be executed by a separate interpreter because
> that is the whole point of shebangs. Including one sends a strong
> signal that that file is supposed to be executed.
It's magic that allows file(1) et al. to make a proper identification.
--zeurkous.
--
Friggin' Machines!
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 21:11 ` zeurkous
@ 2021-09-01 21:19 ` Bart Schaefer
2021-09-01 21:29 ` Lawrence Velázquez
0 siblings, 1 reply; 53+ messages in thread
From: Bart Schaefer @ 2021-09-01 21:19 UTC (permalink / raw)
To: zeurkous; +Cc: Lawrence Velázquez, Ray Andrews, Zsh Users
On Wed, Sep 1, 2021 at 2:11 PM <zeurkous@volny.cz> wrote:
>
> Haai,
>
> "Lawrence Velázquez" <larryv@zsh.org> wrote:
> > Similarly, I don't think you should include a shebang in a file
> > that is not meant to be executed by a separate interpreter because
> > that is the whole point of shebangs. Including one sends a strong
> > signal that that file is supposed to be executed.
>
> It's magic that allows file(1) et al. to make a proper identification.
Also used by smart editors to initialize syntax coloring.
The ONLY trustworthy signal that a file is supposed to be executed
should be that it's execute permission bit is set.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 21:19 ` Bart Schaefer
@ 2021-09-01 21:29 ` Lawrence Velázquez
0 siblings, 0 replies; 53+ messages in thread
From: Lawrence Velázquez @ 2021-09-01 21:29 UTC (permalink / raw)
To: Bart Schaefer, zeurkous; +Cc: Ray Andrews, zsh-users
On Wed, Sep 1, 2021, at 5:19 PM, Bart Schaefer wrote:
> On Wed, Sep 1, 2021 at 2:11 PM <zeurkous@volny.cz> wrote:
> > "Lawrence Velázquez" <larryv@zsh.org> wrote:
> > > Similarly, I don't think you should include a shebang in a file
> > > that is not meant to be executed by a separate interpreter because
> > > that is the whole point of shebangs. Including one sends a strong
> > > signal that that file is supposed to be executed.
> >
> > It's magic that allows file(1) et al. to make a proper identification.
>
> Also used by smart editors to initialize syntax coloring.
>
> The ONLY trustworthy signal that a file is supposed to be executed
> should be that it's execute permission bit is set.
Well sure. I don't buy into adding shebangs to code just to help
tools out (clearly), but that's just a personal preference.
--
vq
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 20:53 ` Lawrence Velázquez
2021-09-01 21:11 ` zeurkous
@ 2021-09-01 21:23 ` Ray Andrews
1 sibling, 0 replies; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 21:23 UTC (permalink / raw)
To: zsh-users
On 2021-09-01 1:53 p.m., Lawrence Velázquez wrote:
> Even before getting to language mismatches, it generally doesn't
> make sense to source a file that has a shebang because such a file
> probably assumes that it'll be run in a separate process.
That's just the point I wanted to clear up. As I now understand it, the
shebang will be completely ignored when sourcing although it had
occurred to me that it might force a separate process. But experiments
here show that does not happen. Steve says the same thing. So, when
'dot' or 'source' starts the command, that what is in the file will be
zsh code is already expected. But when we execute, nothing is presumed
and the shebang is read and a separate process is started. Quite
sensible.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 20:03 ` Bart Schaefer
2021-09-01 20:52 ` Steve Dondley
2021-09-01 20:53 ` Lawrence Velázquez
@ 2021-09-01 21:05 ` Ray Andrews
2 siblings, 0 replies; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 21:05 UTC (permalink / raw)
To: zsh-users
On 2021-09-01 1:03 p.m., Bart Schaefer wrote:
>
> No. "#!" is a comment, because it begins with "#".
>
I've always wondered about the syntax of that. Seemed to me it was the
one exception. But ... what you're saying is that the shells themselves
ignore the shebang as just another comment, but when you 'execute' the
file, a closer look it taken, yes? So, if I source a file, the shebang
is 'just another comment' but if I execute it, the shebang is paid
attention to because there is no presumption as to the correct
interpreter -- could be Python, could be anything.
> ... superficial
> similarities so that incorrect interpreters can recognize themselves
> and do a backflip, but for the most part it is indeed "barf."
Sounds like a whole lot more trouble than it could be worth. For now
what I'm coming away with is that the shebang is only relevant if I have
a 'chmod +x' file and execute it as tho it were a binary. And sourcing
is not the same thing as 'executing'. I've not done any of this
executing of anything zsh yet, so up till now the issue has not come up,
but I'm now forewarned, many thanks.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 2:33 ` Ray Andrews
2021-09-01 3:01 ` Bart Schaefer
@ 2021-09-01 3:19 ` Steve Dondley
2021-09-01 3:30 ` Bart Schaefer
2021-09-01 3:27 ` Lawrence Velázquez
2 siblings, 1 reply; 53+ messages in thread
From: Steve Dondley @ 2021-09-01 3:19 UTC (permalink / raw)
To: Ray Andrews; +Cc: zsh-users
> Yikes, I'm missing something fundamental. In my head, executing a
> script is identical to sourcing it. It's not a binary, so what else
> can there be than interpreting the file? If a function is declared
> (yes I know, wrong word but you get my meaning) then the function
> enters memory, otherwise it's interpret-and-forget until next time,
> no? If 'chmod +x' then you don't hafta 'say' 'source me', it's
> automatic, and it will be found if on the path, etc, but I'm laboring
> under the idea that nothing else is different. Is this going to
> hurt? I don't understand the lesson of Roman's example, so I think it
> is going to hurt.
The way I think about it is sourcing is basically like including the
body of the text that's in the sourced file into your configuration file
(or whatever file is sourcing it). After everything is sourced, it all
gets dumped into memory. Nothing is executed, unless, of course, you
actually have a bit of code in your sourced files that runs some
commands which will get executed during the sourcing process.
Scripts should not be sourced at all. Those have commands that will be
executed. Roman is just saying to call those scripts directly. For
example, I plan on calling them from aliases. His argument is that the
hassle of setting up functions is too high and it's much easier just to
use scripts which can use any programming language rather than having to
be written in zsh. This also makes it much easier to switch to another
shell.
I believe functions are not stored in memory until they are called at
least once in zsh if they are loaded with "autoload." I could be
entirely wrong on this so someone please tell me if I am.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 3:19 ` Steve Dondley
@ 2021-09-01 3:30 ` Bart Schaefer
0 siblings, 0 replies; 53+ messages in thread
From: Bart Schaefer @ 2021-09-01 3:30 UTC (permalink / raw)
To: Steve Dondley; +Cc: Ray Andrews, Zsh Users
On Tue, Aug 31, 2021 at 8:19 PM Steve Dondley <s@dondley.com> wrote:
>
> I believe functions are not stored in memory until they are called at
> least once in zsh if they are loaded with "autoload."
Essentially correct. An abbreviated version is stored that provides
some details about how (and sometimes where) to load the "real"
content. For a sufficiently small function, you're better off just
defining the function directly.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 2:33 ` Ray Andrews
2021-09-01 3:01 ` Bart Schaefer
2021-09-01 3:19 ` Steve Dondley
@ 2021-09-01 3:27 ` Lawrence Velázquez
2021-09-01 16:20 ` Ray Andrews
2 siblings, 1 reply; 53+ messages in thread
From: Lawrence Velázquez @ 2021-09-01 3:27 UTC (permalink / raw)
To: Ray Andrews; +Cc: zsh-users
On Tue, Aug 31, 2021, at 10:33 PM, Ray Andrews wrote:
> On 2021-08-31 5:28 p.m., Bart Schaefer wrote:
> > Sourcing a file is not the same as executing a script.
> >
> Yikes, I'm missing something fundamental. In my head, executing a
> script is identical to sourcing it.
If you want to go a little deeper than Bart's summary, here's some
light bedtime reading.
https://homepages.cwi.nl/~aeb/std/hashexclam-1.html
https://www.in-ulm.de/~mascheck/various/shebang/
> If a function is declared (yes I know, wrong word but you get my
> meaning) then the function enters memory, otherwise it's
> interpret-and-forget until next time, no?
I don't really know what you mean here.
> If 'chmod +x' then you don't hafta 'say' 'source me', it's
> automatic, and it will be found if on the path, etc, but I'm
> laboring under the idea that nothing else is different.
It's trivial to confirm that this is not the case.
% cat ./foo
#!/bin/zsh
foo=2
typeset -p foo
% chmod +x ./foo
% foo=1
% typeset -p foo
typeset foo=1
% ./foo
typeset foo=2
% typeset -p foo
typeset foo=1
% . ./foo
typeset foo=2
% typeset -p foo
typeset foo=2
--
vq
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 3:27 ` Lawrence Velázquez
@ 2021-09-01 16:20 ` Ray Andrews
2021-09-01 18:34 ` Lawrence Velázquez
0 siblings, 1 reply; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 16:20 UTC (permalink / raw)
To: zsh-users
On 2021-08-31 8:27 p.m., Lawrence Velázquez wrote:
>
> If you want to go a little deeper than Bart's summary, here's some
> light bedtime reading.
>
> https://homepages.cwi.nl/~aeb/std/hashexclam-1.html
"The first group, /arg0/, consists of one argument. For SysVR4, SunOS,
Solaris, IRIX, BSDI, BSD-OS, OpenBSD, DU, Unixware, Linux 2.4, FreeBSD
this argument is |/path/interpreter|. For Tru64 (4.0), AIX (4.3, 5.1),
Linux 2.2, MacOS X this argument is |interpreter|. For HP-UX this
argument is |/scriptpath/script|."
... I am in awe of the mind of the person who compiles this sort of
information.
> https://www.in-ulm.de/~mascheck/various/shebang/
"On many other flavours, the maximum length varies between
_POSIX_PATH_MAX (255) and PATH_MAX (f.i. 1024); see |limits.h| or
|syslimits.h| on the respective system.Exceptions are BIG-IP4.2
(BSD/OS4.1) with 4096 and FreeBSD since 6.0 (PAGE_SIZE) with 4096 or
8192 depending on the architecture.Minix also uses the limit of
|PATH_MAX| characters (255 here) but the actual limit is 257
characters,because |patch_stack()| in |src/mm/exec.c| first skips the
"|#!|" with an |lseek()| and then reads in the rest. "
... Good to know!
>
>> If a function is declared (yes I know, wrong word but you get my
>> meaning) then the function enters memory, otherwise it's
>> interpret-and-forget until next time, no?
> I don't really know what you mean here.
When we source a function it's there in memory for next time, no?
Otherwise a script is freshly read each time it is sourced. I"m pretty
sure about this.
> It's trivial to confirm that this is not the case.
>
Indeed, I'm now aware of the difference but I sure don't understand
it. I had no idea there was any such issue.
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 16:20 ` Ray Andrews
@ 2021-09-01 18:34 ` Lawrence Velázquez
2021-09-01 19:19 ` Ray Andrews
0 siblings, 1 reply; 53+ messages in thread
From: Lawrence Velázquez @ 2021-09-01 18:34 UTC (permalink / raw)
To: Ray Andrews; +Cc: zsh-users
On Wed, Sep 1, 2021, at 12:20 PM, Ray Andrews wrote:
> On 2021-08-31 8:27 p.m., Lawrence Velázquez wrote:
> >> If a function is declared (yes I know, wrong word but you get my
> >> meaning) then the function enters memory, otherwise it's
> >> interpret-and-forget until next time, no?
> > I don't really know what you mean here.
> When we source a function it's there in memory for next time, no?
Singling out functions obscures what is going on. All changes to
the sourcing shell's state persist, not just function definitions.
> Otherwise a script is freshly read each time it is sourced. I"m pretty
> sure about this.
The file is always read each time it is sourced (setting aside .zwc
files and what not). It does not matter whether it defines functions
or not.
(I really hope you are not conflating sourced files with autoloaded
functions.)
--
vq
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: Best practices for managing aliases in ohmyzsh?
2021-09-01 18:34 ` Lawrence Velázquez
@ 2021-09-01 19:19 ` Ray Andrews
0 siblings, 0 replies; 53+ messages in thread
From: Ray Andrews @ 2021-09-01 19:19 UTC (permalink / raw)
To: zsh-users
On 2021-09-01 11:34 a.m., Lawrence Velázquez wrote:
> The file is always read each time it is sourced (setting aside .zwc
> files and what not). It does not matter whether it defines functions
> or not.
I think I'm clear about this. I'm just saying that once a function has
been sourced, it's in memory. As was pointed out earlier, if you just
run scripts you keep your memory resources a bit trimmer at the expense
of a micro second here and there. Anyway the panic is over -- a line of
code is a line of code, function, sourced or executable, all the same.
Functions: same environment. Sourced or executable: fresh environment.
^ permalink raw reply [flat|nested] 53+ messages in thread