Menu

#17 PS1 command substitution

1.0
open
nobody
None
2020-04-19
2014-02-02
No

http://stackoverflow.com/q/21517281

This command succeeds

$ PS1='$(date +%s) $ '
1391380852 $

However if I add a newline it fails

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

If I use backticks it works

$ PS1='`date +%s`\n$ '
1391381008
$

but backticks are discouraged. So what is causing this error?

Discussion

  • Kjartan Fredrik Kvamme

    I'm running into the same thing when trying to use the git-prompt in MSYS2.

    $ PS1='\w $(__git_ps1)\n $ '
    bash: command substitution: line 1: syntax error near unexpected token `)'
    bash: command substitution: line 1: `__git_ps1)'
    /d/Source/testing
     $ 
    

    The exact same prompt is working fine on another machine running Arch Linux, which also happens to be using the same version of bash (4.3.22).

    $ PS1='\w $(__git_ps1)\n $ '
    ~/source/chandler  (code-cleanup)
     $
    

    If I remove the newline after the command substitution, it works.

    $ PS1='\w $(__git_ps1) $ '
    /d/Source/testing  (develop) $
    

    It seems like $() style command substitution for some reason always fails in MSYS2 if a newline is present after it.

     
  • Matthieu Vachon

    Matthieu Vachon - 2014-08-22

    Here my own PS1 that works:

    # User PS1 Customization
    export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w$(__git_ps1)'$'\n\$\[\e[0m\] '
    

    It's hard to read but it works. I derive this using the answer provided in the stack overflow question above.

    I don't know why it is not working as expected however.

    Regards,
    Matt

     
    • Kjartan Fredrik Kvamme

      Putting any newlines after the command substitution in its own quotes with a dollar sign in front of it does indeed avoid the error, as does using backticks.
      Still seems odd that this is necessary when the same thing works fine on other platforms though.

       
    • Andreas Larsen

      Andreas Larsen - 2014-11-05

      I tried your PS1, but I still get this error:

      bash: __git_ps1: command not found

      Any ideas? I just installed msys2 on Windows 8.1 today so I'm not very familiar with it.

      UPDATE: Fixed it by copying in git-prompt.sh and git-completion.bash files from msysgit to my /etc folder and adding the following to my .profile:

      . /etc/git-completion.bash
      [ -r /etc/git-prompt.sh ] && . /etc/git-prompt.sh

       

      Last edit: Andreas Larsen 2014-11-05
  • Matthieu Vachon

    Matthieu Vachon - 2014-11-05

    I was about to reply exactly this. Glad you solved your problem.

    Regards,
    Matt

     
  • David Macek

    David Macek - 2017-02-05
    • status: open --> closed
     
  • David Macek

    David Macek - 2017-02-05

    Huh, seems to work now. Closing.

     
  • David Macek

    David Macek - 2020-04-19
    • status: closed --> open
     
  • David Macek

    David Macek - 2020-04-19

    And now it doesn't. Opening. :/

     
MongoDB Logo MongoDB