Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bash $'ANSI-C quoted strings' in child_process.exec #37725

Closed
verhovsky opened this issue Mar 12, 2021 · 3 comments
Closed

Support bash $'ANSI-C quoted strings' in child_process.exec #37725

verhovsky opened this issue Mar 12, 2021 · 3 comments
Labels
child_process Issues and PRs related to the child_process subsystem.

Comments

@verhovsky
Copy link

verhovsky commented Mar 12, 2021

What steps will reproduce the bug?

const exec = require('child_process').exec;

exec("printf '%s' $'hello'", (error, stdout, stderr) => console.log(JSON.stringify(stdout)));

How often does it reproduce? Is there a required condition?

Always?

What is the expected behavior?

$ printf '%s' $'hello'
hello$ 

(there's no newline when using printf, that's why the $ is at the end instead of on the next line)

What do you see instead?

$ node /tmp/above_code_snippet.js
"$hello"
$

Additional information

ANSI strings in bash are documented here https://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting and work like this:

$ echo $'some\\ntext'
some\ntext
$ echo 'some\\ntext'
some\\ntext

See also yargs/yargs-parser#346

@PoojaDurgad PoojaDurgad added the child_process Issues and PRs related to the child_process subsystem. label Mar 12, 2021
@aduh95
Copy link
Contributor

aduh95 commented Mar 12, 2021

I might be wrong but I think this feature is a bashism, not part of the standard POSIX shell. Note that the default shell is /bin/sh on Unix-like systems (see documentation), so you might want to specify a different one (probably the result of command -v bash).

import { exec } from 'child_process';

exec('command -v bash', (err, stdout) =>
  exec(
    "printf '%s' $'hello'",
    { shell: stdout.trim() },
    (error, stdout, stderr) => console.log(JSON.stringify(stdout))
  )
);

Anyway, I don't think this is a Node.js bug, exec is passing the string given straight to the shell, so there's nothing we can do here. Generally one would prefer to stick to POSIX-compliant shell code to ensure the code works everywhere, but if you are not concerned about distributability my suggested code should do just fine.

@RaisinTen
Copy link
Contributor

I might be wrong but I think this feature is a bashism, not part of the standard POSIX shell.

I don't think this is a bug. This feature certainly does not work on sh:

$ echo $0
sh
$ printf '%s' $'hello'
$hello

@aduh95
Copy link
Contributor

aduh95 commented Mar 12, 2021

I'm going to close this as working as intended. Do not hesitate to ask more questions if needed or re-open if we misunderstood your issue.

@aduh95 aduh95 closed this as completed Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants