msys2 / MSYS2-packages

Package scripts for MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
1.28k stars 483 forks source link

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

Open k-takata opened 4 years ago

k-takata commented 4 years ago

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 commented 4 years ago

If I revert the following hunks, the issue disappears:

https://github.com/msys2/MSYS2-packages/blob/8a97a6ca34414680f59b2227d3df223c65e90373/bash/0005-bash-4.3-msys2-fix-lineendings.patch#L86-L93 https://github.com/msys2/MSYS2-packages/blob/8a97a6ca34414680f59b2227d3df223c65e90373/bash/0005-bash-4.3-msys2-fix-lineendings.patch#L182-L189 https://github.com/msys2/MSYS2-packages/blob/8a97a6ca34414680f59b2227d3df223c65e90373/bash/0005-bash-4.3-msys2-fix-lineendings.patch#L197-L200

stirwl commented 1 year ago

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

GeekDuanLian commented 9 months ago

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 commented 9 months ago

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

akinomyoga commented 5 months ago

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 https://github.com/msys2/MSYS2-packages/issues/1839#issuecomment-1751613961 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 commented 3 months ago

@lazka Who is responsible for the Bash package?

ElectricRCAircraftGuy commented 1 month ago

Workarounds and more info on Stack Overflow:

  1. My Q&A: Cannot use command substitution inside Git Bash on Windows: "bash: command substitution: line 1: syntax error near unexpected token `)'"
  2. PS1 command substitution fails when containing newlines on msys bash
k-takata commented 1 month ago

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