mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 15:04:05 +08:00
28 lines
2.0 KiB
HTML
28 lines
2.0 KiB
HTML
<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='([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")'
|
||
declare -ir EUID="0"
|
||
declare -ir PPID="31436"
|
||
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
|
||
declare -ir UID="0"</code></pre>
|
||
<p>对于只读变量而言,若用户对其值进行修改,则会立即报错。例如,使用该指令定义一个只读变量“test”,并且将其值初始化为“ok”,输入如下命令:</p>
|
||
<pre><code class="language-bash">[root@localhost ~]# readonly test='ok' #定义只读变量并初始化 </code></pre>
|
||
<p>那么当用户直接修改该只读变量时,就会被报错,如下所示:</p>
|
||
<pre><code class="language-bash">[root@localhost ~]# test='my' #试图修改只读变量的值
|
||
-bash: test: readonly variable</code></pre>
|
||
<p>当用户试图修改只读变量的值时,会被提示该变量为只读变量。</p>
|
||
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
|