Skip to content
New issue

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

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

Already on GitHub? # to your account

bash: '\n' after command substitution in $PS1 causes error #1839

Open
k-takata opened this issue Jan 27, 2020 · 8 comments · May be fixed by #4477
Open

bash: '\n' after command substitution in $PS1 causes error #1839

k-takata opened this issue Jan 27, 2020 · 8 comments · May be fixed by #4477

Comments

@k-takata
Copy link
Contributor

When I use \n after command substitution in $PS1, it causes syntax error:

$ PS1='$(date)\n\$ '
-bash: command substitution: line 1: syntax error near unexpected token `)'
-bash: command substitution: line 1: `date)'

It should not cause the error.
If I remove the \n after $(date), it works fine:

$ PS1='$(date)\$ '
Mon Jan 27 19:16:55 JST 2020$ 

Using backquotes instead of $() also works fine:

$ PS1='`date`\n\$ '
Mon Jan 27 19:21:51 JST 2020
$ 

Setting PS1='$(date)\n\$ ' works fine on Cygwin, so this seems an MSYS2 specific issue.

See also: https://stackoverflow.com/questions/21517281/ps1-command-substitution-fails-when-containing-newlines-on-msys-bash

@k-takata
Copy link
Contributor Author

If I revert the following hunks, the issue disappears:

- return (*(bash_input.getter)) ();
+#ifdef __MSYS__
+ int c;
+ while ((c = (*(bash_input.getter)) ()) == '\r');
+ return c;
+#else
+ return (*(bash_input.getter)) ();
+#endif

- return (*(bash_input.getter)) ();
+#ifdef __MSYS__
+ int c;
+ while ((c = (*(bash_input.getter)) ()) == '\r');
+ return c;
+#else
+ return (*(bash_input.getter)) ();
+#endif

+#ifdef __MSYS__
+ if (c == '\r')
+ continue;
+#endif

@stirwl
Copy link

stirwl commented Jul 14, 2023

I have same issue too.
My bash version is 5.2.15(1)-release (x86_64-pc-msys)

@GeekDuanLian
Copy link

I recently switched from Cygwin to MSYS2 and I have the same problem, and this problem seems to be only on MSYS2, I don't experience it on any of the remaining platforms.

Hope this issue can be fixed soon.

@stirwl
Copy link

stirwl commented Oct 7, 2023

I add \e[0m\]\n at the line end. It works fine.

@akinomyoga
Copy link
Contributor

This hasn't yet been fixed after four years since the report. Did anyone make an effort to fix this problem? @lazka @Alexpux Who are responsible for the Bash package in MSYS2?

This causes many error reports as follows. This is not a complete list. These are just the reports to a repository that I maintain and some others that can be quickly found by Google search.

For individual personal setting, "the fix" suggested in some of the threads or #1839 (comment) might work. However, we don't want to include in a larger shared framework such specific workarounds caused by a package unmaintained for ten years unless there is a reason that this cannot be fixed in the origin.

@akinomyoga
Copy link
Contributor

@lazka Who is responsible for the Bash package?

@k-takata k-takata linked a pull request Mar 31, 2024 that will close this issue
@k-takata
Copy link
Contributor Author

As I wrote in #4477, the issue also occurs in Cygwin bash when the igncr option is set.
I've sent a patch to the Cygwin mailing list:
https://cygwin.com/pipermail/cygwin/2024-May/255980.html

k-takata added a commit to k-takata/MSYS2-packages that referenced this issue Oct 2, 2024
Fixes msys2#1839

`0005-bash-4.3-msys2-fix-lineendings.patch` adds CRLF support.
However, `0001-bash-4.4-cygwin.patch` already added `igncr` option to
Bash to support CRLF.

I confirmed that the Cygwin version of Bash has also the same issue
with msys2#1839 when the `igncr` option is set.

After debugging, I found that there is an issue in
`rewind_input_string()` in `parser.y` that it doesn't take the CR into
account.

This PR adds the following changes:
* Modify `rewind_input_string()` to take the CR into account.
  (It might be better to apply a similar change to the Cygwin version of
  Bash.)
* Remove all the changes from `y.tab.c`. This file should be
  automatically generated from `parser.y`.
k-takata added a commit to k-takata/MSYS2-packages that referenced this issue Feb 10, 2025
Fixes msys2#1839

`0005-bash-4.3-msys2-fix-lineendings.patch` adds CRLF support.
However, `0001-bash-4.4-cygwin.patch` already added `igncr` option to
Bash to support CRLF.

I confirmed that the Cygwin version of Bash has also the same issue
with msys2#1839 when the `igncr` option is set.

After debugging, I found that there is an issue in
`rewind_input_string()` in `parser.y` that it doesn't take the CR into
account.

This PR adds the following changes:
* Modify `rewind_input_string()` to take the CR into account.
  (It might be better to apply a similar change to the Cygwin version of
  Bash.)
* Remove all the changes from `y.tab.c`. This file should be
  automatically generated from `parser.y`.
* Set `LC_ALL=C.UTF-8` when running `make check` for running on
  non-English locales.
* Add two tests: `run-ps1lf` and `run-crlf`.

Note: This patch contains a line with CRLF. So, .gitattributes is also
updated to keep the CRLF.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants