<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="https://shaozk.xyz/feed_style.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <tabi:metadata xmlns:tabi="https://github.com/welpo/tabi">
        <tabi:base_url>https:&#x2F;&#x2F;shaozk.xyz</tabi:base_url>
        <tabi:separator>
            •
        </tabi:separator>
        <tabi:about_feeds>This is a web feed, also known as an Atom feed. Subscribe by copying the URL from the address bar into your newsreader. Visit About Feeds to learn more and get started. It&#x27;s free.</tabi:about_feeds>
        <tabi:visit_the_site>Visit website</tabi:visit_the_site>
        <tabi:recent_posts>Recent posts</tabi:recent_posts>
        <tabi:last_updated_on>Updated on $DATE</tabi:last_updated_on>
        <tabi:default_theme></tabi:default_theme>
        <tabi:post_listing_date>date</tabi:post_listing_date>
        <tabi:current_section>shell</tabi:current_section>
    </tabi:metadata><title>shaozk - shell</title>
    <link href="https://shaozk.xyz/tags/shell/atom.xml" rel="self" type="application/atom+xml"/>
    <link href="https://shaozk.xyz/tags/shell/" rel="alternate" type="text/html"/>
    <generator uri="https://www.getzola.org/">Zola</generator><updated>2026-10-14T00:00:00+00:00</updated><id>https://shaozk.xyz/tags/shell/atom.xml</id><entry xml:lang="en">
        <title>调用其他 Shell 脚本的函数</title>
        <published>2026-10-14T00:00:00+00:00</published>
        <updated>2026-10-14T00:00:00+00:00</updated>
        <author>
            <name>shaozk</name>
        </author>
        <link rel="alternate" href="https://shaozk.xyz/blog/028-call-other-shell-func/" type="text/html"/>
        <id>https://shaozk.xyz/blog/028-call-other-shell-func/</id>
        
            <content type="html">&lt;p&gt;当 Shell 脚本比较大的时候，常用的做法是拆成多个文件：抽取一些常用的函数（类似面向对象编程的工具类）集中放在一起，以供其他脚本调用，从而拥有更清晰的结构，也更便于后续维护。&lt;&#x2F;p&gt;
&lt;p&gt;在一般的编程语言中，我们可以通过引入的方法来实现这一行为。如 C&#x2F;C++ 中 include 头文件的方法、Python 中使用的 import 语句。那 Shell 脚本中该如何实现呢？&lt;&#x2F;p&gt;
&lt;h2 id=&quot;source-nei-zhi-ming-ling&quot;&gt;source 内置命令&lt;&#x2F;h2&gt;
&lt;p&gt;source 是一个 Shell 的内置命令，作用为在当前环境下读取指定的脚本内容并运行其中的命令，用该命令可以实现这一点。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# func.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-shebang z-shell z-meta z-shebang z-shell&quot;&gt;#!&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;func&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function z-builtin&quot;&gt;  echo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;hello&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# call_func.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-shebang z-shell z-meta z-shebang z-shell&quot;&gt;#!&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function z-builtin&quot;&gt;source&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; func.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;func&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;yong-bash-xing-bu-xing&quot;&gt;用 bash 行不行？&lt;&#x2F;h2&gt;
&lt;p&gt;bash 也可以运行脚本命令，那能不能使用呢？答案是否定的，因为 bash 执行脚本会新建子 Shell，无法影响当前 Shell 的环境。如果将上文代码中 source 直接替换为 bash，执行将会报错：找不到 func 命令。&lt;&#x2F;p&gt;
&lt;p&gt;这里一定要注意 bash 和 source 的区别，source 在当前环境运行，bash 在子进程环境运行。如果我是面试官，这很好出题。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;ru-he-gei-zi-jin-cheng-chuan-di-han-shu&quot;&gt;如何给子进程传递函数？&lt;&#x2F;h2&gt;
&lt;p&gt;我们知道使用 bash 命令会在子进程运行，那么如果我们的需求是将当前进程的函数传递给子进程呢？&lt;&#x2F;p&gt;
&lt;p&gt;可以使用 export 内置命令，并使用 -f 选项指定需要传递的函数。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# export_func.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-shebang z-shell z-meta z-shebang z-shell&quot;&gt;#!&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function z-builtin&quot;&gt;source&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; func.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;export&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; func&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;bash&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; -c &amp;#39;func&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;zong-jie&quot;&gt;总结&lt;&#x2F;h2&gt;
&lt;p&gt;虽然现在使用 AI 可以快速写出你需要的脚本，但我想基础只是也是不能没有的，不要让 AI 蒙蔽了你的双眼。&lt;&#x2F;p&gt;
&lt;p&gt;&quot;曾经，人类把思考交给机器，希望这样能让自己自由。但那只是让拥有机器的另一些人得以奴役他们。&quot; - 《沙丘》&lt;&#x2F;p&gt;
</content>
        </entry>
</feed>
