Skip to content

Commit 9e01f42

Browse files
committed
Disallow var syntax in command interpolations
This is special cased for compatibility. A more general fix would be to make cmd interpolation syntax exactly the same as string interpolation.
1 parent 2ca4b31 commit 9e01f42

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

base/shell.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ function shell_parse(str::AbstractString, interpolate::Bool=true;
7070
isempty(st) && error("\$ right before end of command")
7171
stpos, c = popfirst!(st)
7272
isspace(c) && error("space not allowed right after \$")
73-
ex, j = Meta.parse(s,stpos,greedy=false)
73+
if SubString(s, stpos:min(stpos+2, ncodeunits(s))) == "var"
74+
# Disallow var"#" syntax in cmd interpolations.
75+
# TODO: Allow only identifiers after the $ for consistency with
76+
# string interpolation syntax (see #3150)
77+
ex, j = :var, stpos+3
78+
else
79+
ex, j = Meta.parse(s,stpos,greedy=false)
80+
end
7481
last_parse = (stpos:prevind(s, j)) .+ s.offset
7582
update_arg(ex);
7683
s = SubString(s, j)

test/spawn.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ let c = setenv(`x`, "A"=>true)
461461
@test_throws ArgumentError `"$c "`
462462
end
463463

464+
# Interaction of cmd parsing with var syntax (#32408)
465+
let var = "x"
466+
@test `ls $var` == Cmd(["ls", "x"])
467+
@test `ls $var"y"` == Cmd(["ls", "xy"])
468+
@test `ls $var "y"` == Cmd(["ls", "x", "y"])
469+
end
470+
464471
# equality tests for AndCmds
465472
@test Base.AndCmds(`$echocmd abc`, `$echocmd def`) == Base.AndCmds(`$echocmd abc`, `$echocmd def`)
466473
@test Base.AndCmds(`$echocmd abc`, `$echocmd def`) != Base.AndCmds(`$echocmd abc`, `$echocmd xyz`)

0 commit comments

Comments
 (0)