0

Background

I want to redirect the result of a one-liner shell script to a file on a terminal in the GitHub Codespaces environment.

For example, when I executed the following one-liner in the Terminal view of VSCode:

$ for i in 1 2 3; do echo $i; done

I got the following result:

for i in 1 2 3; do echo $i; done123$

Problem

When I redirected the result of the same one-liner:

$ for i in 1 2 3; do echo $i; done > foo.txt

I got the foo.txt like this. It contains escape sequences and the for ... in clause of the one-liner:

]633;C]633;E;for\x20i\x20in\x201\x202\x203123

]633;C]633;E;for\x20i\x20in\x201\x202\x2031
2
3

When I ran the same one-liner in my WSL2 environment, the result didn't contain these headings.

Question

How can I stop the redirected output containing the for in clause on the GitHub Codespaces environment?

SATO Yusuke
  • 1,600
  • 15
  • 39
  • 1
    Report this as a bug to github. There are no circumstances under which this is correct behavior; even if xtrace behavior or similar is desired, none of that belongs on stdout. (Terminal control sequences, debug tracing, etc are all supposed to be on stderr, which `>` doesn't redirect). – Charles Duffy Dec 09 '22 at 17:07
  • That said, `echo $i` is itself buggy -- always use `echo "$i"`, _with the quotes_, for the reasons given in [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Dec 09 '22 at 17:08
  • ...in the interim, might I suggest you switch from github codespaces to https://repl.it/? Still an online editor/sandbox, but one that's been around a lot longer, written by people who understand UNIX rather than by Microsoft; and consequently much less likely to be broken in ways (like those you show here) that reflect mistakes no UNIX developer would ever make. – Charles Duffy Dec 09 '22 at 17:10
  • 1
    Thank you for your advice. I have reported this to GitHub Community: https://github.com/orgs/community/discussions/41320 . I have bumped into this bug in a Codespaces environment provided by a course in LinkedIn Learning, so I can't move to repl.it. – SATO Yusuke Dec 19 '22 at 15:22
  • 1
    As a hack to use until they get that fixed... if you run `for x in 1 2 3; do echo "$x" >&3; done 3>foo.txt`, does that have the same problem? (That way, we're still opening `foo.txt` only once for the whole loop so we keep the original code's efficiency, but we only redirect stdout to it while `echo` is running) – Charles Duffy Dec 19 '22 at 16:13

0 Answers0