]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
runtime(sh): allow "#" in special derefs master
authorD. Ben Knoble <ben.knoble+github@gmail.com>
Tue, 21 Apr 2026 19:59:07 +0000 (19:59 +0000)
committerChristian Brabandt <cb@256bit.org>
Tue, 21 Apr 2026 19:59:07 +0000 (19:59 +0000)
Code like ${!#} flags the "#" as shDerefWordError [1]; the "!prefix"
syntax region delegates to one of the shDerefSpecial handlers via
@shDerefList, but it misses the "#" case as valid for ${##} and ${!#}.

[1]: https://vi.stackexchange.com/q/48617/10604

Correct that. Indirection is only valid in Bash in Ksh, so rearrange the
"!" handling to be conditional.

closes: #20016

Helped-by: Christian Brabandt <cb@256bit.org>
Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/syntax/sh.vim
runtime/syntax/testdir/dumps/sh_03_01.dump
runtime/syntax/testdir/dumps/sh_03_02.dump
runtime/syntax/testdir/dumps/sh_bash_00.dump
runtime/syntax/testdir/dumps/sh_bash_01.dump
runtime/syntax/testdir/dumps/sh_bash_02.dump
runtime/syntax/testdir/input/sh_03.sh
runtime/syntax/testdir/input/sh_bash.bash

index 05eb488d534a8b0b85cf953101b9d28f1cd5473d..d022059d728414dfaf0d193a044e3234a6e9eae6 100644 (file)
@@ -24,6 +24,7 @@
 "              2026 Feb 15 improve comment handling #19414
 "              2026 Mar 23 improve matching of function definitions #19638
 "              2026 Apr 02 improve matching of function definitions #19849
+"              2026 Apr 19 improve detection of special variables #20016
 " }}}
 " Version:             208
 " Former URL:          http://www.drchip.org/astronaut/vim/index.html#SYNTAX_SH
@@ -751,13 +752,15 @@ endif
 if exists("b:is_bash")
     syn region shDeref matchgroup=PreProc start="\${!" end="\*\=}"     contains=@shDerefList,shDerefOffset
     syn match  shDerefVar      contained       "{\@<=!\h\w*"           nextgroup=@shDerefVarList
+    syn match  shDerefSpecial  contained       "\({!\)\@<=[[:alnum:]*#@_]\+"   nextgroup=@shDerefVarList,shDerefOp
 endif
 if (exists("b:is_kornshell") && !exists("b:is_ksh88"))
     syn match  shDerefVar      contained       "{\@<=!\h\w*[[:alnum:]_.]*"     nextgroup=@shDerefVarList
+    syn match  shDerefSpecial  contained       "\({!\)\@<=[[:alnum:]*#@_]\+"   nextgroup=@shDerefVarList,shDerefOp
 endif
 
 syn match  shDerefSpecial      contained       "{\@<=[-*@?0]"          nextgroup=shDerefOp,shDerefOffset,shDerefOpError
-syn match  shDerefSpecial      contained       "\({[#!]\)\@<=[[:alnum:]*@_]\+" nextgroup=@shDerefVarList,shDerefOp
+syn match  shDerefSpecial      contained       "\({[#]\)\@<=[[:alnum:]*@_]\+"  nextgroup=@shDerefVarList,shDerefOp
 syn match  shDerefVar  contained       "{\@<=\h\w*"            nextgroup=@shDerefVarList
 syn match  shDerefVar  contained       '\d'                            nextgroup=@shDerefVarList
 if exists("b:is_kornshell") || exists("b:is_posix")
index 3ac5b4bc7af0b0b7194369ab6bf50f39d9d49f6b..81e2c87de5f1ac90a1e1eb8f258bd941685649d3 100644 (file)
@@ -17,4 +17,4 @@
 |:+0#0000e05&| +0#0000000&|'+0#af5f00255&|$+0#e000002&|{|V|a|r|i|a|b|l|e|B|:|-|$|{|V|a|r|i|a|b|l|e|C|:|-|e|n|g|}@1|'+0#af5f00255&| +0#0000000&@39
 @75
 |#+0#0000e05&| |A|n|o|t|h|e|r| |t|e|s|t| +0#0000000&@60
-@57|1|8|,|0|-|1| @7|8|1|%| 
+@57|1|8|,|0|-|1| @7|6|5|%| 
index 6d03697c7b9db56bb1687496fa1596fb723ef61c..f34582e496b167352bea663d2bf20d3b3ad73cb6 100644 (file)
@@ -1,7 +1,11 @@
 |#+0#0000e05#ffffff0| |A|n|o|t|h|e|r| |t|e|s|t| +0#0000000&@60
 |V+0#00e0e07&|a|r|i|a|b|l|e|=+0#0000000&|$+0#e000e06&|{|V|a|r|i|a|b|l|e|B|:+0#af5f00255&|-|$+0#e000e06&|{|V|a|r|i|a|b|l|e|C|:+0#af5f00255&|-|$+0#e000e06&|{|V|a|r|i|a|b|l|e|D|:+0#af5f00255&|-|$+0#e000e06&|{|V|a|r|i|a|b|l|e|E|:+0#af5f00255&|=|e+0#0000000&|n|g|}+0#e000e06&@3| +0#0000000&@6
 @7|:+0#0000e05&| +0#0000000&@7|$+0#e000e06&|{|V|a|r|i|a|b|l|e|B|:+0#af5f00255&|=|$+0#e000e06&|{|V|a|r|i|a|b|l|e|C|:+0#af5f00255&|-|$+0#e000e06&|{|V|a|r|i|a|b|l|e|D|:+0#af5f00255&|-|$+0#e000e06&|{|V|a|r|i|a|b|l|e|E|:+0#af5f00255&|=|e+0#0000000&|n|g|}+0#e000e06&@3
-> +0#0000000&@74
+| +0#0000000&@74
+|#+0#0000e05&| |s|p|e|c|i|a|l| |v|a|r|i|a|b|l|e|s| +0#0000000&@55
+>e+0#af5f00255&|c|h|o| +0#e000002&|"+0#af5f00255&|$+0#e000e06&|{|!|#|}|"+0#af5f00255&| +0#e000002&@11|#+0#0000e05&| |l|a|s|t| |p|o|s|i|t|i|o|n|a|l| |a|r|g|u|m|e|n|t| +0#0000000&@24
+|e+0#af5f00255&|c|h|o| +0#e000002&|"+0#af5f00255&|$+0#e000e06&|{|!|@|}|"+0#af5f00255&| +0#e000002&@11|#+0#0000e05&| |d|e|r|e|f| +0#0000000&@43
+|e+0#af5f00255&|c|h|o| +0#e000002&|"+0#af5f00255&|$+0#e000e06&|{|#|P|A|T|H|}|"+0#af5f00255&| +0#e000002&@8|#+0#0000e05&| |l|e|n|g|t|h| +0#0000000&@42
 |~+0#4040ff13&| @73
 |~| @73
 |~| @73
@@ -13,8 +17,4 @@
 |~| @73
 |~| @73
 |~| @73
-|~| @73
-|~| @73
-|~| @73
-|~| @73
-| +0#0000000&@56|3@1|,|0|-|1| @7|B|o|t| 
+| +0#0000000&@56|3|5|,|1| @9|B|o|t| 
index 85966ebd75a7714a5cc68438e64a816c629358ec..15093831d703674e5b7b4aa279e2fc0f1d0ffabb 100644 (file)
@@ -14,7 +14,7 @@
 |e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|s+0#e000002&|e|v|e|n|'+0#af5f00255&| +0#e000002&@3|;+0#e000e06&|}| +0#0000000&@48
 |e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|e+0#e000002&|i|g|h|t|'+0#af5f00255&|;+0#e000e06&| @2|}| +0#0000000&@49
 |t+0#af5f00255&|y|p|e|s|e|t| +0#e000e06&|n+0#00e0e07&|i|n|e|=+0#0000000&|$+0#e000e06&|{| |p+0#af5f00255&|w|d|;| +0#e000e06&|}| +0#0000000&@52
-|e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|n+0#e000002&|i|n|e|'+0#af5f00255&| +0#e000002&|;+0#e000e06&| | +0#0000000&@52
+|e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|n+0#e000002&|i|n|e|'+0#af5f00255&| +0#e000002&|;+0#e000e06&| +0#0000000&@53
 | +0#e000e06&|}| +0#0000000&@72
 @75
 |i|s|_|b|a|s|h|:| |1|,| @45|1|,|1| @10|T|o|p| 
index 18961a7983859f4e2e32a3d87448d8f2308490e2..1a65e23fc2e666599a44581f9b8b3fcdf010095c 100644 (file)
@@ -1,7 +1,7 @@
 |e+0#af5f00255#ffffff0|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|s+0#e000002&|e|v|e|n|'+0#af5f00255&| +0#e000002&@3|;+0#e000e06&|}| +0#0000000&@48
 |e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|e+0#e000002&|i|g|h|t|'+0#af5f00255&|;+0#e000e06&| @2|}| +0#0000000&@49
 |t+0#af5f00255&|y|p|e|s|e|t| +0#e000e06&|n+0#00e0e07&|i|n|e|=+0#0000000&|$+0#e000e06&|{| |p+0#af5f00255&|w|d|;| +0#e000e06&|}| +0#0000000&@52
-|e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|n+0#e000002&|i|n|e|'+0#af5f00255&| +0#e000002&|;+0#e000e06&| | +0#0000000&@52
+|e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|{| |e+0#af5f00255&|c|h|o| +0#e000002&|'+0#af5f00255&|n+0#e000002&|i|n|e|'+0#af5f00255&| +0#e000002&|;+0#e000e06&| +0#0000000&@53
 | +0#e000e06&|}| +0#0000000&@72
 > @74
 |v+0#e000e06&|a|l|s|u|b|f|u|n|c|(|)| |{| +0#0000000&@60
@@ -12,9 +12,9 @@
 |p+0#af5f00255&|r|i|n|t|f| +0#e000e06&|'+0#af5f00255&|%+0#e000002&|s|\|n|'+0#af5f00255&| +0#e000e06&|"+0#af5f00255&|$+0#e000e06&|{|||v|a|l|s|u|b|f|u|n|c| |t|w|e|l|v|e| @4|;|}|"+0#af5f00255&| +0#0000000&@31
 |u+0#00e0e07&|n|l|u|c|k|y|=+0#0000000&|$+0#e000e06&|{||+0#af5f00255&|v+0#e000e06&|a|l|s|u|b|f|u|n|c| |t|h|i|r|t|e@1|n| +0#0000000&@44
 |}+0#e000e06&| +0#0000000&@73
-|t+0#af5f00255&|y|p|e|s|e|t| +0#e000e06&|n+0#00e0e07&|o|t|a|f|l|o|a|t|=+0#0000000&|$+0#e000e06&|{||+0#af5f00255&|v+0#e000e06&|a|l|s|u|b|f|u|n|c| |n|o|t|a|n|u|m|b|e|r| @5|;+0#af5f00255&| +0#e000e06&| +0#0000000&@24
+|t+0#af5f00255&|y|p|e|s|e|t| +0#e000e06&|n+0#00e0e07&|o|t|a|f|l|o|a|t|=+0#0000000&|$+0#e000e06&|{||+0#af5f00255&|v+0#e000e06&|a|l|s|u|b|f|u|n|c| |n|o|t|a|n|u|m|b|e|r| @5|;+0#af5f00255&| +0#0000000&@25
 | +0#e000e06&|}| +0#0000000&@72
 |e+0#af5f00255&|c|h|o| +0#e000002&|$+0#e000e06&|u|n|l|u|c|k|y| +0#e000002&|$+0#e000e06&|n|o|t|a|n|u|m|b|e|r| +0#0000000&@49
 |$+0#e000e06&|{||+0#af5f00255&|e|c|h|o| +0#e000002&|f|o|u|r|t|e@1|n|;+0#af5f00255&|}+0#e000e06&| +0#0000000&@56
 |$+0#e000e06&|{||+0#af5f00255&|e|c|h|o| +0#e000002&|f|i|f|t|e@1|n| +0#0000000&@59
-@57|1|9|,|0|-|1| @7|9|2|%| 
+@57|1|9|,|0|-|1| @7|7|2|%| 
index 677a1ab127b9bd67d70d0eba47d1fa9ab7d560a8..f7ea1a30017e8230ed3a0303450b800daa95dddb 100644 (file)
@@ -1,5 +1,9 @@
 |$+0#e000e06#ffffff0|{||+0#af5f00255&|e|c|h|o| +0#e000002&|f|i|f|t|e@1|n| +0#0000000&@59
->}+0#e000e06&| +0#0000000&@73
+|}+0#e000e06&| +0#0000000&@73
+@75
+|e+0#af5f00255&|c|h|o| +0#e000002&|"+0#af5f00255&|$+0#e000e06&|{|!|#|}|"+0#af5f00255&| +0#e000002&@11|#+0#0000e05&| |l|a|s|t| |p|o|s|i|t|i|o|n|a|l| |a|r|g|u|m|e|n|t| +0#0000000&@24
+|e+0#af5f00255&|c|h|o| +0#e000002&|"+0#af5f00255&|$+0#e000e06&|{|!|@|}|"+0#af5f00255&| +0#e000002&@11|#+0#0000e05&| |d|e|r|e|f| +0#0000000&@43
+>e+0#af5f00255&|c|h|o| +0#e000002&|"+0#af5f00255&|$+0#e000e06&|{|#|P|A|T|H|}|"+0#af5f00255&| +0#e000002&@8|#+0#0000e05&| |l|e|n|g|t|h| +0#0000000&@42
 |~+0#4040ff13&| @73
 |~| @73
 |~| @73
@@ -13,8 +17,4 @@
 |~| @73
 |~| @73
 |~| @73
-|~| @73
-|~| @73
-|~| @73
-|~| @73
-| +0#0000000&@56|3@1|,|1| @9|B|o|t| 
+| +0#0000000&@56|3|7|,|1| @9|B|o|t| 
index 8dd6dab9c379b4a89d888cd8282c36aed4831d13..d2ac8f0a4ab9df02ff0f78dd37ff08397c464eb6 100644 (file)
@@ -31,3 +31,7 @@ Variable="${VariableB:-${VariableC:-eng}}"  # :- is ksh and bash
 Variable=${VariableB:-${VariableC:-${VariableD:-${VariableE:=eng}}}}
        :        ${VariableB:=${VariableC:-${VariableD:-${VariableE:=eng}}}}
 
+# special variables
+echo "${!#}"           # last positional argument
+echo "${!@}"           # deref
+echo "${#PATH}"                # length
index 35b536c4e38b0b4ff6f874f829c3288983f3278b..798ce44ebe5c82c036d6f8c34a1259106b25cb9c 100644 (file)
@@ -14,7 +14,7 @@ echo ${ echo 'six'
 echo ${        echo 'seven'    ;}
 echo ${ echo 'eight';  }
 typeset nine=${ pwd; }
-echo ${ echo 'nine' ; 
+echo ${ echo 'nine' ;
  }
 
 valsubfunc() {
@@ -25,9 +25,13 @@ echo "${|valsubfunc eleven; }"
 printf '%s\n' "${|valsubfunc twelve    ;}"
 unlucky=${|valsubfunc thirteen
 }
-typeset notafloat=${|valsubfunc notanumber     ; 
+typeset notafloat=${|valsubfunc notanumber     ;
  }
 echo $unlucky $notanumber
 ${|echo fourteen;}
 ${|echo fifteen
 }
+
+echo "${!#}"           # last positional argument
+echo "${!@}"           # deref
+echo "${#PATH}"                # length