diff --git a/lib/elixir/lib/system.ex b/lib/elixir/lib/system.ex index 9b958b0086..dfc4556564 100644 --- a/lib/elixir/lib/system.ex +++ b/lib/elixir/lib/system.ex @@ -955,6 +955,12 @@ defmodule System do @doc since: "1.12.0" @spec shell(binary, keyword) :: {Collectable.t(), exit_status :: non_neg_integer} def shell(command, opts \\ []) when is_binary(command) do + command |> String.trim() |> do_shell(opts) + end + + defp do_shell("", _opts), do: {"", 0} + + defp do_shell(command, opts) do assert_no_null_byte!(command, "System.shell/2") {close_stdin?, opts} = Keyword.pop(opts, :close_stdin, false) diff --git a/lib/elixir/test/elixir/system_test.exs b/lib/elixir/test/elixir/system_test.exs index dde86e5a2c..56f9c84df7 100644 --- a/lib/elixir/test/elixir/system_test.exs +++ b/lib/elixir/test/elixir/system_test.exs @@ -209,6 +209,11 @@ defmodule SystemTest do assert {"1\n2\n", 0} = System.shell("x=1; echo $x; echo '2'") end + test "shell/1 with empty string" do + assert {"", 0} = System.shell("") + assert {"", 0} = System.shell(" ") + end + @tag timeout: 1_000 test "shell/1 returns when command awaits input" do assert {"", 0} = System.shell("cat", close_stdin: true)