special meaning of ^ (caret) in solaris (bourne?) sh?

北城余情 提交于 2019-12-24 01:03:06

问题


On Solaris, it appears I need to single quote 'a match the beginning of line' expression:

 > sh
 $ echo offset 0.000000 2>&1 | grep ^offset | tail -1
 offset: not found
 $ Usage:        grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
         grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f pattern_file]... [file...]
         grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
         grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]
         grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
         grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]

I can fix this easily enough by single quoting the regex:

 > sh
 $ echo offset 0.000000 2>&1 | grep '^offset'| tail -1
 offset 0.000000

I don't see this in my own command line where I'm using bash, but it shows up in scripts, such as a perl script that uses system().

Is that default Solaris shell the Bourne shell? What additional meaning does ^ (caret) have in the shell language for the default Solaris shell?


回答1:


I believe '^' in bourne shell was allowed as a backwards compatible version/equivalent of the '|' char.

And the message that your seeing would seems to support that interpretation.

To confirm, make a test script offset that just prints 'got to offset' to see.

(Backwards compatible to what is lost in this ol' SunOS3 coder's /dev/null ;-) )

IHTH.



来源:https://stackoverflow.com/questions/13125406/special-meaning-of-caret-in-solaris-bourne-sh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!