在 Jupyter Notebook 中执行终端命令时出现意外结果

Unexpected result while executing terminal command in Jupyter notebook

在 jupyter notebook 中有这个单元格运行正常:

!echo "one two tree"|egrep --color "t[w,r](o|e)"

return(如预期):一个 两个 tree

当此单元格输入时:

!echo "one two tree"|egrep --color "t[w,r](o|e){1,2}"

returnjupyter 上什么都没有?!
预期return:一棵两棵树

我试过转义大括号

!echo "one two tree"|egrep --color "t[w,r](o|e)\{1,2\}"

return

上仍然没有内容

问题是您在字符串中使用 cause variable expansion 的大括号:

The line after the bang can call any program installed in the underlying shell, and support variable expansion in the form of $variable or {variable}.

因此,要使大括号文字,您需要将它们加倍:

egrep "t[wr](o|e){{1,2}}"