Skip to content

Unable to pipe Bash output to other programs #8

Closed

Description

@RaeesBhatti

I'm unable to spawn Bash from NodeJS. If you try to, it will return this error: Error: 0x80070057

How to reproduce:

  • Install NodeJS
  • Run node in CMD
  • Type:
cp = require('child_process');
proc = cp.spawnSync('cmd', ['/c', 'bash -c "ls"'], {encoding: 'utf8'}).output

It outputs:

[ null, 'Error: 0x80070057\r\r\n', '' ]

Activity

  1. justinhixson commented on Apr 7, 2016

    @justinhixson

    This is actually a pretty interesting one. in Python, for example, I can spawn bash just fine with the subprocess module, but only under certain circumstances. consider the following script:

    import subprocess
    subprocess.run(["bash","-c","ls"])
    

    this is saved as bash.py

    c:\>python bash.py works

    c:\>python
    >>import subprocess;
    >>subprocess.run(["bash","-c","ls"])
    

    also works

    however, I found it very interesting that

    C:\>echo import subprocess; subprocess.run(["bash","-c","ls"]); | python 
    

    does NOT work. same Error: 0x80070057

    (note that something like

    C:\>echo import subprocess; subprocess.run(["notepad"]); | python
    

    does indeed work.)

    I was using python 3.5 for these tests.

    I am sure that it is much more complicated than this, but bash.exe seems to be really, really offended by stream redirection with pipes. that doesn't clearly explain why OP is having a problem with node.js, but note that this is exactly the same error that you would get if you ever try to pipe something into bash.exe at all. for instance:

    C:\Users\Justin>type rocks.sh
    #!/bin/bash
    echo "bash on Windows ????!" | sed 's/????/rocks/g'
    
    C:\Users\Justin>bash rocks.sh
    bash on Windows rocks!
    
    C:\Users\Justin>type rocks.sh | bash
    Error: 0x80070057
    
    C:\Users\Justin>type rocks.sh | bash -c "sh"
    Error: 0x80070057
    
    C:\Users\Justin>bash -c "sh"
    # rocks.sh
    bash on Windows rocks!
    # exit
    
    C:\Users\Justin>bash -c "sh" < rocks.sh
    Error: 0x80070057
    
    C:\Users\Justin>echo "./rocks.sh" | bash -c "sh"
    Error: 0x80070057
    
  2. RaeesBhatti commented on Apr 7, 2016

    @RaeesBhatti
    Author

    Can confirm. This is a problem with bash having problems with output piping. If I specify { stdio: 'inhert' }, it works but that way I then have no way of getting that data. { stdio: 'pipe' } produces the error above

  3. changed the title [-]Unable to spawn Bash from other programs[/-] [+]Unable to pipe Bash output to other programs[/+] on Apr 7, 2016
  4. MadcowD commented on May 1, 2016

    @MadcowD

    I've made a related issue in Bash on Windows. #312

  5. bitcrazed commented on Jun 22, 2017

    @bitcrazed
    Contributor

    Thanks for the issue report - this was fixed in Creators Update.

  6. justinhixson commented on Jun 23, 2017

    @justinhixson

    Thank you so much for this! This has been my most anticipated feature for the Linux Subsystem since i first heard about it. I am enjoying it immensely, and can confirm that it now works exactly as one would imagine it would. Thanks again!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions