zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Steve Dondley <s@dondley.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Best practices for managing aliases in ohmyzsh?
Date: Tue, 31 Aug 2021 19:57:41 +0200	[thread overview]
Message-ID: <CAN=4vMothtLeSvHdF_5jvcQmLKdtZPriitSef4x=12Rs5SdkhQ@mail.gmail.com> (raw)
In-Reply-To: <1ee1545904fdc1b5f473460cddfe5000@dondley.com>

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.


  reply	other threads:[~2021-08-31 18:03 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-30 18:13 Steve Dondley
2021-08-30 22:27 ` Ray Andrews
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  1:29       ` Steve Dondley
2021-08-31  8:26     ` Roman Perepelitsa
2021-08-31 15:51       ` Steve Dondley
2021-08-31 17:57         ` Roman Perepelitsa [this message]
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:43               ` Steve Dondley
2021-08-31 21:44             ` Roman Perepelitsa
2021-09-01  0:12           ` Ray Andrews
2021-09-01  0:28             ` Bart Schaefer
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 18:22                     ` Lawrence Velázquez
2021-09-01 19:11                       ` Ray Andrews
2021-09-01 19:16                         ` Bart Schaefer
2021-09-01 19:56                           ` Ray Andrews
2021-09-01 20:03                             ` Bart Schaefer
2021-09-01 20:52                               ` Steve Dondley
2021-09-01 21:06                                 ` Ray Andrews
2021-09-01 22:06                                   ` Steve Dondley
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:29                                     ` Lawrence Velázquez
2021-09-01 21:23                                 ` Ray Andrews
2021-09-01 21:05                               ` Ray Andrews
2021-09-01  3:19                 ` Steve Dondley
2021-09-01  3:30                   ` Bart Schaefer
2021-09-01  3:27                 ` Lawrence Velázquez
2021-09-01 16:20                   ` Ray Andrews
2021-09-01 18:34                     ` Lawrence Velázquez
2021-09-01 19:19                       ` Ray Andrews
2021-08-31  8:06 ` Anssi Saari
2021-08-31 15:45   ` Steve Dondley
2021-08-31 16:24 ` zzapper
2021-08-31 16:47   ` Steve Dondley
2021-08-31 17:20     ` Steve Dondley
2021-08-31 17:26     ` Bart Schaefer
2021-08-31 17:40       ` Steve Dondley
2021-08-31 17:47       ` Steve Dondley
2021-08-31 18:31         ` Ray Andrews
2021-08-31 18:59         ` Lawrence Velázquez
2021-09-01  6:40 ` Michael Klemm
2021-09-01 22:13   ` Steve Dondley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN=4vMothtLeSvHdF_5jvcQmLKdtZPriitSef4x=12Rs5SdkhQ@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=s@dondley.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).