Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(bash) $((Base#number)) variable syntax issue #1504

Closed
GHGibonus opened this issue Apr 26, 2017 · 4 comments · Fixed by #2439
Closed

(bash) $((Base#number)) variable syntax issue #1504

GHGibonus opened this issue Apr 26, 2017 · 4 comments · Fixed by #2439
Labels
help welcome Could use help from community language

Comments

@GHGibonus
Copy link

GHGibonus commented Apr 26, 2017

In bash, you can specify the base of a number in an arithmetic expression with the construct B#n where the value of that construct is n is base B (B can be anything from 2 to 32, or can be specified with a $variable).

The issue is that in highlight.js, in this configuration, # is considered as the start of a comment. Here are three examples use cases:

echo $(( 16#deadbeef / 1003 ))
yumi=deadbeef
echo $(( 16#$yumi / 1003 ))
B=20
yumi=deadbeef
echo $(( $B#$yumi / 1003 ))

What I see in hightlight js: everything after the # is a comment.

What I expect: 16# and $B# should be considered as a numeric literal or something similar (at least when inside the $((...)) arithmetic expression construct); And stuff after it should not be considered a comment. Typically, I expect normal syntax highlight to work after 16#.

Some tests gives me:

> hljs = require("highlight.js")
> hljs.highlightAuto("echo $(( 16#deadbeef * 10))").value
'<span class="hljs-built_in">echo</span> $(( 16<span class="hljs-comment">#deadbeef * 10))</span>'

Note: this is the original snippet of code that prompted me to open this issue:

#!/bin/sh

#Sets the current system date to one hour before now.
#WARNING!! don't use after midnight.
curhour=$(date +'%H')
curdate=$((10#$curhour - 1)):$(date +'%M:%S')
date --set="$curdate"
@joshgoebel
Copy link
Member

Is it even possible to have comments inside of subexpressions I wonder?

@joshgoebel joshgoebel changed the title Bash: $((Base#number)) syntax issue (bash) $((Base#number)) variable syntax issue Oct 13, 2019
@joshgoebel joshgoebel added help welcome Could use help from community language labels Oct 24, 2019
@joshgoebel
Copy link
Member

So If there is whitespace around it can I actually put a comment inside an $(( )) block?

@egor-rogov
Copy link
Collaborator

I believe no. man bash says:

  Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result.  The format for arithmetic expansion is:

         $((expression))

  The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially.   All  tokens  in
  the  expression undergo parameter and variable expansion, command substitution, and quote removal.  The result is treated as the arithmetic expres‐
  sion to be evaluated.  Arithmetic expansions may be nested.

As the expression "is treated as if it were within double quotes", comments can't be allowed in it.

@joshgoebel
Copy link
Member

Awesome. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help welcome Could use help from community language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants