在支持数组变量的类似 Bourne 的 shell 中,我们可以使用一些解析来检查变量是否为数组。
下面的所有命令都是在运行后运行的a=(1 2 3)
。
zsh
:
$ declare -p a
typeset -a a
a=( 1 2 3 )
Run Code Online (Sandbox Code Playgroud)
bash
:
$ declare -p a
declare -a a='([0]="1" [1]="2" [2]="3")'
Run Code Online (Sandbox Code Playgroud)
ksh93
:
$ typeset -p a
typeset -a a=(1 2 3)
Run Code Online (Sandbox Code Playgroud)
pdksh
及其衍生物:
$ typeset -p a
set -A a
typeset a[0]=1
typeset a[1]=2
typeset a[2]=3
Run Code Online (Sandbox Code Playgroud)
yash
:
$ typeset -p a
a=('1' '2' '3')
typeset a
Run Code Online (Sandbox Code Playgroud)
一个例子bash
:
if declare -p var 2>/dev/null …
Run Code Online (Sandbox Code Playgroud) 该yash
外壳有一个printf
内置的,根据其说明书。
但是,这是我在yash
具有默认配置的shell 中看到的:
$ command -v printf
/usr/bin/printf
$ type printf
printf: a regular built-in at /usr/bin/printf
Run Code Online (Sandbox Code Playgroud)
printf
这个shell中是否内置了?结果与许多其他假定的内置实用程序类似,这些实用程序也可用作外部命令。
作为对比,在pdksh
(ksh
在OpenBSD,这里printf
是不是内置的):
$ command -v printf
/usr/bin/printf
$ type printf
printf is /usr/bin/printf
Run Code Online (Sandbox Code Playgroud)
并在bash
(其中printf
是内置的):
$ command -v printf
printf
$ type printf
printf is a shell builtin
Run Code Online (Sandbox Code Playgroud)