From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 13207 invoked from network); 31 Aug 2021 18:03:07 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 31 Aug 2021 18:03:07 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1630432987; b=eg/q5KaC3zxeY8pSRAf9oXi7gGYntwQ8P1v5kQd/mKKenyahp9hzFvfn2p6IurgqXdkYbvl/pK qWs81MI7sVpAmF026IkdDyZ9eSD/O6SCjkm48iqkkNRP2GlPB/yxJ9vb28dtU9/Jc3h4fMc8Sy 6R0AiGNOf93gaPaj+rmXfdooyLrPUmOMyU73N2FBv1m8RoojmTE1Acx3tLVcgV6tpbVB7i0OgP cwiZHIX8rlqyCqo4CuYaVSleeXRjPDMmCgdjDUmghXGDsBc31qhN6NcwtwDvWFsrprmiWPRfs9 cTJsuiUovWpcpCUB4VoUnj2WhAFlvoxCCHP4+Ml18hMNnQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-qt1-f171.google.com) smtp.remote-ip=209.85.160.171; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1630432987; bh=quhkuFPOO83XUhSVPbWjCY+9FzHkWjYbas9JYt1MR5E=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=hNuVWqiGN5yvgKMnrzXYML8T5gVOxHucuc149d28kgliGbwOtHx+P54lG51DmQd1zr8/BQrbN/ aMPkntawNHzESf196nXkYlZTGYxDOcjWIOcP/znjhv6D2OMOk3E/qvebD1D3bBWOT11lBxL0hM eDsJkjYf4zFkTW3JQss8++4ajzg9Gz+zlSCSSRl2PZFh4/eX94yDVSIWkd/heINTT8nlTd+yfl 0Eg2FzVeUpzUJK2VOMX0aQtqA3W9DbGzHMoMYn9dPOjzzJjgf7113/9pwOoTDF9CNDOCT8908E VzOcuPyITtereycrhNDbjb39exFrqhSd5jwzBdmFPMidTQ==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=W2p5Rr2l9DcdqLSbIbKtqaBQI9PMDP3HqMhsM3m5ntk=; b=HX4jUgsIGriTZBoxCXE8W3z2K4 1JpU0r+mKb4bLaBL8LYfZwbee9lcl5vf12rqgrMZlMHH5l+C54o3qhvK5jN0JpJCA6FMUB9oiMJQ/ d4lIkjemGAILb0DQ9v8k8sqDHEESTkLP6/w4mEl/GbpirJG9XI4fnjYn1Eoak2DGjnpE19wDbIPbm Stdzx3qkb3VL9Q7cPgb4s+OmiHb+0szxF7KxDFJ/OKbGBTcdMTjrph2BhUG7cGkdVfpP37CLWKHrV nOKPhXIpsfSuFtbOEhe3umTufsDvbKIu+IS2lD+9j9gnD0Yu1qjFZJxkbRZQwcLbrPRtNMTaHRg3N 4zWh6AUg==; Received: from authenticated user by zero.zsh.org with local id 1mL86F-000BWb-3e; Tue, 31 Aug 2021 18:03:07 +0000 Authentication-Results: zsh.org; iprev=pass (mail-qt1-f171.google.com) smtp.remote-ip=209.85.160.171; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-qt1-f171.google.com ([209.85.160.171]:44922) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1mL81B-000A81-0e; Tue, 31 Aug 2021 17:57:53 +0000 Received: by mail-qt1-f171.google.com with SMTP id r21so229411qtw.11 for ; Tue, 31 Aug 2021 10:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=W2p5Rr2l9DcdqLSbIbKtqaBQI9PMDP3HqMhsM3m5ntk=; b=RCJFq5yzGfpG8kVg0JwBpO240DlgScRSN/uY0SLAhYA5AM3MWNABF3meeRg+Wn09VH BlJojZjToDXZHp22Tsbxh8Z5AFNpQm8I9ZPG2OOzkEPCuWT6+12vNS/+VLUrLjOgt4R7 UHy8/TmC0W8xydmTXVKzqh6WvdxzFgVYomKQRMJyXd9CvrWpT9jz4aQsieHPXdNasrml +4ziMxVx196L1oZHduNL4TdYugq/mxnEr09vb0Yp1f80D7wYt9AOetzCE2cfIs+i1qDp xFmlM1je90Qn6Pw/Odx0Wch0LTOkBfPJigXimlXeoOGFr2M5vgmpkYwhUEuLS+EdHiKu gKmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=W2p5Rr2l9DcdqLSbIbKtqaBQI9PMDP3HqMhsM3m5ntk=; b=FvRhVxrZ26Hm93L8kW8XJUe76i3dlSTaSHfEqmkvPuX3L283NVm94mQvGc4kbNkKO4 PIkn7SEDwXG5Gfl8kRdb6rdRBjA4mruQ6ymXDUoW8I4kRZWvCMNeq1alVSzQKbUELyqZ i9PcHc5XCtjrK7q2idubh0manG4lndG6RlY35cp0a6aB75i5JrzGA8FTBLox9fHm1Iq7 SDwrvZjxW/d7ygTlFE4KFGsNjz8YSW5Zxv/YiTPQuwfljwqNZ7lp5EGm9j1n/k1YpoGW GYd8oXU1tNBFfxDHNJ4OqXQ6/xnUvkKICVpol9Hd4TNVneUD5D/mkmJ9tznGfwHywWOz mFIw== X-Gm-Message-State: AOAM532Rmrg6WxrF1Wnc5EQF+DylY+TLexUIc1LbypfKVqo+oJROlASG xtdzK0RZH1lsBYxLCdRWvdhC0gOPBDhpIspTVuU= X-Google-Smtp-Source: ABdhPJy3fSz95sVDIjqZMC/I9NrTDDm6MN/q0aq+GDQZrJA95crYWRsCViESPSy1TXs9AptD78nMV1ALHo9hvO035UA= X-Received: by 2002:a05:622a:1:: with SMTP id x1mr4009399qtw.198.1630432671888; Tue, 31 Aug 2021 10:57:51 -0700 (PDT) MIME-Version: 1.0 References: <41fa241f119fdb9b8598293aad345823@dondley.com> <776abe09c579095258b2eef28a7ad5ca@dondley.com> <1ee1545904fdc1b5f473460cddfe5000@dondley.com> In-Reply-To: <1ee1545904fdc1b5f473460cddfe5000@dondley.com> From: Roman Perepelitsa Date: Tue, 31 Aug 2021 19:57:41 +0200 Message-ID: Subject: Re: Best practices for managing aliases in ohmyzsh? To: Steve Dondley Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" X-Seq: 27025 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: On Tue, Aug 31, 2021 at 5:52 PM Steve Dondley 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.