@@ -920,19 +920,26 @@ defmodule System do
920
920
921
921
## Options
922
922
923
- It accepts the same options as `cmd/3`, except for `arg0`.
923
+ It accepts the same options as `cmd/3` (except for `arg0`).
924
+ It also accepts the following exclusive options:
925
+
926
+ * `:close_stdin` (since v1.14.1) - if the stdin should be closed
927
+ on Unix systems, forcing any command that waits on stdin to
928
+ immediately terminate. Defaults to false.
924
929
"""
925
930
@ doc since: "1.12.0"
926
931
@ spec shell ( binary , keyword ) :: { Collectable . t ( ) , exit_status :: non_neg_integer }
927
932
def shell ( command , opts \\ [ ] ) when is_binary ( command ) do
928
933
assert_no_null_byte! ( command , "System.shell/2" )
934
+ { close_stdin? , opts } = Keyword . pop ( opts , :close_stdin , false )
929
935
930
936
# Finding shell command logic from :os.cmd in OTP
931
937
# https://github.com/erlang/otp/blob/8deb96fb1d017307e22d2ab88968b9ef9f1b71d0/lib/kernel/src/os.erl#L184
932
938
case :os . type ( ) do
933
939
{ :unix , _ } ->
934
- shell_path = :os . find_executable ( 'sh' ) || :erlang . error ( :enoent , [ command , opts ] )
935
- command = "(#{ command } \n ) </dev/null"
940
+ shell_path = :os . find_executable ( ~c" sh" ) || :erlang . error ( :enoent , [ command , opts ] )
941
+ close_stdin = if close_stdin? , do: " </dev/null" , else: ""
942
+ command = IO . iodata_to_binary ( [ "(" , command , "\n )" , close_stdin ] )
936
943
do_cmd ( { :spawn_executable , shell_path } , [ args: [ "-c" , command ] ] , opts )
937
944
938
945
{ :win32 , osname } ->
0 commit comments