1

Is this behaviour of jq, w.r.t the non-allowed use of $module as a variable name, specified anywhere?

$ jq -n --arg 'module' 'X' '$module'
jq: error: syntax error, unexpected module, expecting IDENT or __loc__ (Unix shell quoting issues?) at <top-level>, line 1:
$module 
jq: 1 compile error

My expectation is that jq would print "X", but this is not the case. Why?

peak
  • 105,803
  • 17
  • 152
  • 177
jonseymour
  • 1,006
  • 1
  • 12
  • 22

1 Answers1

1

In jq 1.6 and earlier, keywords cannot be used in $-variable names. This is mentioned on the the jq wiki, where the following list of keywords is given:

__loc__ and as break catch def elif else end 
foreach if import include label module
or reduce then try

The list of keywords for any particular version of jq can be derived from the lexer.l file, the “master” version of which is https://github.com/stedolan/jq/blob/master/src/lexer.l

——

The restriction was removed in the development version of jq in 2019 by https://github.com/jqlang/jq/commit/c72ed135e4f1b5a02a8fb3f6cd46f27513ecab2a and thus will no longer exist in jq 1.7

peak
  • 105,803
  • 17
  • 152
  • 177
  • Hopefully someone with better _fu_ can derive it better than this: [`curl -s https://github.com/jqlang/jq/blob/master/src/lexer.l | jq -r 'reduce .payload.blob.rawLines[] as $line ([]; . += [$line|select(test("^\"[$_a-z]+\".*return [A-Z_]+;") and (test("^\"(true|false|null)\"")|not))|capture("\"(?[$_a-z]+)\"")|.out])|sort|.[]'`](https://jqplay.org/s/RbW0VFrrKlh "link to jqplay.org -->"). – rickhg12hs Jul 22 '23 at 14:20