Skip to content

Commit

Permalink
Correctly print subshells nested in a variable expansion (#32)
Browse files Browse the repository at this point in the history
Fix: Nested shell in subshell

Signed-off-by: Bolun Thompson <[email protected]>
  • Loading branch information
BolunThompson authored Jan 3, 2025
1 parent b7d9236 commit 3258c95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 8 additions & 1 deletion libdash/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,14 @@ def string_of_arg_char (c, quote_mode=UNQUOTED):
elif (type == "Q"):
return "\"" + string_of_arg (param, quote_mode=QUOTED) + "\"";
elif (type == "B"):
return "$(" + to_string (param) + ")";
body = to_string (param)
# to handle $( () )
try:
if body[0] == "(" and body[-1] == ")":
body = f" {body} "
except IndexError:
pass
return "$(" + body + ")"
else:
abort ();

Expand Down
8 changes: 6 additions & 2 deletions ocaml/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,12 @@ and string_of_arg_char ?quote_mode:(quote_mode=QUnquoted) = function
| V (vt,nul,name,a) ->
"${" ^ name ^ (if nul then ":" else "") ^ string_of_var_type vt ^ string_of_arg ~quote_mode a ^ "}"
| Q a -> "\"" ^ string_of_arg ~quote_mode:QQuoted a ^ "\""
| B t -> "$(" ^ to_string t ^ ")"

| B t ->
let s = to_string t in
if String.length s >= 2 && s.[0] = '(' && s.[String.length s - 1] = ')' then
"$( " ^ s ^ " )"
else
"$(" ^ s ^ ")"
and string_of_arg ?quote_mode:(quote_mode=QUnquoted) = function
| [] -> ""
| c :: a ->
Expand Down
4 changes: 4 additions & 0 deletions test/tests/nested_shell_in_subshell.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
( (echo abc) )
echo $( (echo abc) )
echo `(echo abc)`
echo $()

0 comments on commit 3258c95

Please sign in to comment.