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

fix(out): escape backslashes in output shell scripts #35

Merged
merged 4 commits into from
Dec 24, 2020

Conversation

Ryooooooga
Copy link
Contributor

@Ryooooooga Ryooooooga commented Dec 23, 2020

abst: This is a suggestion that the \ contained in the output of pmy main should be escaped.

Currently, backslashes in the output of pmy main are not escaped.
For example, ~/.pmy/rules/pmy_rules.yml is:

- description: test
  regexp-left: ^(?P<query>.*)$ # <query> matches whole input
  cmd-groups:
    - stmt: print 'A\\nB' # print 'A\\nB' should output `A\nB'
      after: cat
  fuzzy-finder-cmd: fzf --query='<query>'
  buffer-left: '<query>'
  buffer-right: '[]'

Then, pmy main outputs the script below:

$ pmy main --buffer-left="" --buffer-right=""
local __pmy_out_command=$'print \'A\\nB\' ;'; ...

This means $__pmy_out_command == print 'A\nB' ;, and eval $__pmy_out_command outputs A<LF>B.

And echo also causes strange behavior.
The escape processing of the echo argument changes depending on the environment.
In the case of zsh, echo performs escaping, but command echo does not.

echo "--sh"
sh -c 'echo "A\nB"'
sh -c 'command echo "A\nB"'

echo "--bash"
bash -c 'echo "A\nB"'
bash -c 'command echo "A\nB"'

echo "--zsh"
zsh -c 'echo "A\nB"'
zsh -c 'command echo "A\nB"'
# Results in Alpine (Docker)
--sh
A\nB
A\nB
--bash
A\nB
A\nB
--zsh
A
B
A\nB

# Results in Ubuntu (Docker)
--sh
A
B
A
B
--bash
A\nB
A\nB
--zsh
A
B
A\nB

# Results in macOS
--sh
A
B
A
B
--bash
A\nB
A\nB
--zsh
A
B
A\nB

Copy link
Owner

@relastle relastle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And echo also causes strange behavior.
The escape processing of the echo argument changes depending on the environment.
In the case of zsh, echo performs escaping, but command echo does not.

This is interesting knowledge I've never known.
Thank you for fixing!

@relastle relastle merged commit 1fd8ce1 into relastle:master Dec 24, 2020
@Ryooooooga Ryooooooga deleted the escape-backslashes branch December 24, 2020 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants