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

2019-07-09 02:37发布

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?

标签: solaris sh
1条回答
狗以群分
2楼-- · 2019-07-09 03:05

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.

查看更多
登录 后发表回答