Joey's shell
您熟悉bash的语法吗?"You're afraid of bugs? Get a bug!" --Joey Tribbiani, "FRIENDS".
想找个mini shell来看看源码,浏览了些网页,有“这玩意也配叫shell?”之感。
- bash-1.14.7.tar.gz 1996-08-29 1.4M
- bash-2.0.tar.gz 1996-12-31 1.3M gcc 12 编译不过。
- bash-3.0.tar.gz 2004-08-03 2.3M gcc 12 编译通过。
Debian 12用的是5.2.15 2022-12-13,最新的是5.3 2025-07-30 11M
搜bash-3.0.tar.gz,国内有镜像,下载速度很快。
补充:土了, apt source bash即可下载到当前目录。
/bin/sh属于dash包 The Debian Almquist Shell (dash) is a POSIX-compliant shell derived from ash. Since it executes scripts faster than bash, and has fewer library dependencies (making it more robust against software or hardware failures), it is used as the default system shell on Debian systems. 这个小,241K dash_0.5.12.orig.tar.gz parser.c没用yacc; arith_yacc.c和arith_lex.c处理算术。 ash (Almquist Shell)是Kenneth Almquist写的。 apt source ash 正在读取软件包列表... 选择dash作为源代码包而非ash 想更小,失败了。如今busybox也不小;低版本的不能开盒即编译成功。
When running configure, the script hangs for about 60 seconds on checking for mktime, eventually fails and moves on.
https://bugs.launchpad.net/adeona/+bug/407735
里面有个parse.y,看懂后自然就明白bash的语法啦!
源码里好像带了y.tab.c,没有yacc能也编译出bash。想修改parse.y再编译,需要apt install yacc.
贴部分代码:
%union {
WORD_DESC *word; /* the word that we read. */
int number; /* the number that we read. */
WORD_LIST *word_list;
COMMAND *command;
REDIRECT *redirect;
ELEMENT element;
PATTERN_LIST *pattern;
}
%start inputunit
inputunit: simple_list simple_list_terminator
| error '\n'
| yacc_EOF
;
redirection: '>' WORD | '<' WORD | NUMBER '>' WORD ..
simple_command_element: WORD
shell_command: for_command
| case_command
| WHILE compound_list DO compound_list DONE
| UNTIL compound_list DO compound_list DONE
| select_command
| if_command
| subshell
| group_command
| arith_command
| cond_command
| arith_for_command
;
for_command: FOR WORD newline_list DO compound_list DONE
| FOR WORD newline_list '{' compound_list '}'
| FOR WORD ';' newline_list DO compound_list DONE
| FOR WORD ';' newline_list '{' compound_list '}'
| FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
| FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
| FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
| FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
| FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
;
arith_for_command: FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
...
if_command: IF compound_list THEN compound_list FI
| IF compound_list THEN compound_list ELSE compound_list FI
| IF compound_list THEN compound_list elif_clause FI
;
我说过for i in 1 2 3; do echo $i; done和ls; ls; ls类似,这个说法很可能是错误的,因为AI说:
- YACC(Yet Another Compiler Compiler)由Stephen C. Johnson于1975年开发
- Bourne Shell(sh)由Stephen Bourne开发,于1979年末随UNIX第7版首次发布
所以sh一开始就用了YACC? 不对,没有sh前,cc yacc.c咋输入?第一个sh是汇编写的吧?
Bourne的音标是/bɔːrn/,电影《谍影重重》男主角的英文名为Jason Bourne(杰森·伯恩)。Bourne有小溪的意思。
str = '' try: while True: str += input().split('#')[0] + ';' except EOFError: pass tokens = [t for t in str.replace(' ', ';').split(';') if len(t)] print(tokens) $ sh.py <.bashrc ['case', '$-', 'in', '*i*)', '*)', 'return', 'esac', 'HISTCONTROL=ignoreboth', 'shopt', '-s', 'histappend', 'HISTSIZE=100', ...
Xonsh is a modern, full-featured and cross-platform python shell. The language is a superset of Python 3.6+ with additional shell primitives that you are used to from Bash and IPython. It works on all major systems including Linux, OSX, and Windows. Xonsh is meant for the daily use of experts and novices.
少些类文科文章罢!

浙公网安备 33010602011771号