uTools-Manuals/docs/linux/readonly.html
2019-04-21 11:50:48 +08:00

28 lines
2.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="readonly">readonly</h1>
<p>定义只读shell变量或函数</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>readonly命令</strong> 用于定义只读shell变量和shell函数。readonly命令的选项-p可以输出显示系统中所有定义的只读变量。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">readonly(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-f定义只读函数
-a定义只读数组变量
-p显示系统中全部只读变量列表。</code></pre>
<h3 id="参数">参数</h3>
<p>变量定义:定义变量,格式为“变量名=‘变量值’”。</p>
<h3 id="实例">实例</h3>
<p>使用readonly命令显示系统中所有的已经定义的只读变量输入如下命令</p>
<pre><code class="language-bash">[root@localhost ~]# readonly #显示只读变量
declare -ar BASH_VERSINFO=&#39;([0]=&quot;3&quot; [1]=&quot;2&quot; [2]=&quot;25&quot; [3]=&quot;1&quot; [4]=&quot;release&quot; [5]=&quot;i686-redhat-linux-gnu&quot;)&#39;
declare -ir EUID=&quot;0&quot;
declare -ir PPID=&quot;31436&quot;
declare -r SHELLOPTS=&quot;braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor&quot;
declare -ir UID=&quot;0&quot;</code></pre>
<p>对于只读变量而言若用户对其值进行修改则会立即报错。例如使用该指令定义一个只读变量“test”并且将其值初始化为“ok”输入如下命令</p>
<pre><code class="language-bash">[root@localhost ~]# readonly test=&#39;ok&#39; #定义只读变量并初始化 </code></pre>
<p>那么当用户直接修改该只读变量时,就会被报错,如下所示:</p>
<pre><code class="language-bash">[root@localhost ~]# test=&#39;my&#39; #试图修改只读变量的值
-bash: test: readonly variable</code></pre>
<p>当用户试图修改只读变量的值时,会被提示该变量为只读变量。</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->