其他 POSIX 兼容的 shell 在多大程度上可以作为 bash 的合理替代品?它们不需要是真正的“插入式”替换,但足够接近以使用大多数脚本并通过一些修改支持其余脚本。
我想要显式的 bash 脚本 - initscripts、DHCP 客户端脚本等 - 以最少的修改工作
我希望我自己收集的更专业的 shell 脚本不需要太多修改
我想拥有字符串操作和内置正则表达式模式匹配等功能
我所知道的唯一认真的竞争者是 zsh 和 mksh。所以,对于在座的那些对他们中的一个或两个都很好的人:
bash 有哪些 zsh 和 mksh 分别没有的特性?
shell 与 bash 共享哪些功能,但使用不兼容的语法?
我已经看到 Fish shell 将进程替换作为一个函数来实现:
# === Fish shell lang:
function psub
mkfifo $the_pipe
cat >$the_pipe &
echo $the_pipe
# remove pipe when bg job is done
end
# Example:
diff (echo abc | psub) (echo def | psub)
Run Code Online (Sandbox Code Playgroud)
完整代码:https : //github.com/fish-shell/fish-shell/blob/master/share/functions/psub.fish
我已经尝试了几个小时为非鱼壳 (mksh) 重新实现它,但无法做到:
# Executable file: my.psub.sh
the_pipe="$(mktemp /tmp/pipe.XXXXXXXXXX)"
mkfifo $the_pipe
cat >$the_pipe &
echo $the_pipe
# Example:
diff $(echo abc | my.psub.sh) $(echo def | my.psub.sh)
Run Code Online (Sandbox Code Playgroud)
命令方块。我已经尝试了我能想到的所有方法,但我不知道下一步该去哪里。
在 Android(使用mkshMirBSD Korn Shell)上,有一种特殊的字符串替换语法(称为“值替换”):
${|commands}
Run Code Online (Sandbox Code Playgroud)
替换结果不是收集命令的输出(如``和),而是从分隔符内分配的变量$()中获取。$REPLY它的特殊之处在于命令不在子 shell 中运行 - 它们在同一个 shell 中运行并且可以访问当前 shell 会话拥有的所有内容。
Debian 有一个mksh适用于 MirBSD Korn Shell 的软件包,其行为与 Android 完全相同。
哪些 shell 支持类似的语法,其中:
如果我开始ksh或mksh,我的向上箭头什么也不做:
$ ksh
$ ^[[A^[[A^[[A^[[A^[[A
Run Code Online (Sandbox Code Playgroud)
但bash如果我开始bash并按下向上箭头,它会起作用。
$ bash
developer@1604:~$ ssh root@127.0.1.2 -p 2223
Run Code Online (Sandbox Code Playgroud)
如果我启动 ksh 或 mksh,我就没有历史记录。我什至设置了 $HISTFILE 变量,如果我启动一个新的 shell,仍然没有历史记录。
我该怎么办?Korn shell 不能记住会话之间的历史记录而 bash shell 可以吗?
如果我喜欢 Korn shell 并且想要一个更好、更广泛的历史记录,是否可以在 ksh 中使用该功能?