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

bash PROMPT_COMMAND syntax error #4645

Closed
FernandoBasso opened this issue Feb 6, 2022 · 10 comments
Closed

bash PROMPT_COMMAND syntax error #4645

FernandoBasso opened this issue Feb 6, 2022 · 10 comments
Labels

Comments

@FernandoBasso
Copy link

FernandoBasso commented Feb 6, 2022

Intro

It seems the the Kitty bash shell integration script is inserting a semicolon on the PROMPT_COMMAND causing it to conflict with what another program is doing with PROMPT_COMMAND. It this case it is with the z program.

Both kitty and z have been installed from the Arch Linux official repositories.

To Reproduce
Steps to reproduce the behavior:

  1. In ~/bashrc, source /usr/share/z/z.sh (this could change depending on the distro or if installed manually in ~/bin or something like it);
  2. Open a fresh kitty instance (it adds some lines to ~/.bashrc:
# BEGIN_KITTY_SHELL_INTEGRATION
if test -n "$KITTY_INSTALLATION_DIR" -a -e "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"; then source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"; fi
# END_KITTY_SHELL_INTEGRATION
  1. See the following bash syntax error message:
bash: PROMPT_COMMAND: line 1: syntax error near unexpected token `;;'
bash: PROMPT_COMMAND: line 1: `(_z --add "$(command pwd -P 2>/dev/null)" 2>/dev/null &);; _ksi_prompt_command'

Note the two semicolons.

Environment details

kitty 0.24.2 created by Kovid Goyal
Linux theforce 5.16.5-arch1-1 #1 SMP PREEMPT Tue, 01 Feb 2022 21:42:50 +0000 x86_64
Arch Linux 5.16.5-arch1-1 (/dev/tty)

LSB_VERSION=1.4
DISTRIB_ID=Arch
DISTRIB_RELEASE=rolling
DISTRIB_DESCRIPTION="Arch Linux"
Running under: X11
Frozen: False
Paths:
  kitty: /usr/bin/kitty
  base dir: /usr/lib/kitty
  extensions dir: /usr/lib/kitty/kitty
  system shell: /bin/bash
Loaded config files:
  /home/fernando/.config/kitty/kitty.conf

I redacted the debug part that seemed to be irrelevant for this topic. The config seems not useful here either. I can provide both if requested.

Additional context

$ declare -p PROMPT_COMMAND 
declare -- PROMPT_COMMAND="printf \"\\033]0;%s@%s:%s\\007\" \"\${USER}\" \"\${HOSTNAME%%.*}\" \"\${PWD/#\$HOME/\\~}\"
(_z --add \"\$(command pwd -P 2>/dev/null)\" 2>/dev/null &);; _ksi_prompt_command"

As we see, there are two semicolons (;;) before _ksi_prompt_command.

Here's an excerpt from /usr/lib/kitty/shell-integration/bash/kitty.bash:

image

If I remove the semicolon highlighted in the screenshot, or if I refrain from sourcing z.sh, the problem disappears.

I have been using z for about 5 years and Kitty for about two years. Both have always worked fine and not interfering with one another. Also, z has not had an upgrade for years. I guess a recent Kitty upgrade added that part of the bash script and the problem came into existence.

Here's an excerpt of /usr/share/z/z.sh bash script:

image

So, z bash script adds a trailing semicolon, while Kitty bash script prepends a semicolon (perhaps defensively in the else case). Note that if I remove that trailing semicolon from z.sh, it simply stops working. That is, if I do z dotfiles, it simply does nothing (it does not change directory to my dotfiles directory). So, it seems it is not a matter of simply removing that semicolon from z.sh.

If I remove the leading semicolon before _ksi_prompt_command, I get no errors, but z doesn't work either. I get no syntax errors, but it seems PROMPT_COMMAND is somehow messed up nonetheless.

I'm not sure if there is a wrong one here. Perhaps it is just a case of bad luck — an unforeseeable situation — or if one of the programs should really change their addition of that specific semicolon something else.

I can create the same syntax error with this:


$ unset PROMPT_COMMAND 

$ (echo hello &); printf 'world'
hello
world

$ (echo hello &);; printf 'world'
bash: syntax error near unexpected token `;;'
@page-down
Copy link
Contributor

I've noticed this before and I thought it had no effect.
I'm curious how to better join the commands in bash.

@securerootd
Copy link

securerootd commented Feb 27, 2022

@kovidgoyal I am using 0.24.2 and with hstr I am getting the same error:

$ -> hstr --show-configuration


# HSTR configuration - add this to ~/.bashrc
alias hh=hstr                    # hh to be alias for hstr
export HSTR_CONFIG=hicolor       # get more colors
shopt -s histappend              # append new history items to .bash_history
export HISTCONTROL=ignorespace   # leading space hides commands from history
export HISTFILESIZE=10000        # increase history file size (default is 500)
export HISTSIZE=${HISTFILESIZE}  # increase history size (default is 500)
# ensure synchronization between bash memory and history file
export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)
if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hstr -- \C-j"'; fi
# if this is interactive shell, then bind 'kill last command' to Ctrl-x k
if [[ $- =~ .*i.* ]]; then bind '"\C-xk": "\C-a hstr -k \C-j"'; fi

bash: PROMPT_COMMAND: line 7: syntax error near unexpected token `;'
bash: PROMPT_COMMAND: line 7: `history -a; history -n; history -a; history -n; ; _ksi_prompt_command'
$ -> kitty -v
kitty 0.24.2 created by Kovid Goyal
bash: PROMPT_COMMAND: line 9: syntax error near unexpected token `;'
bash: PROMPT_COMMAND: line 9: `history -a; history -n; history -a; history -n; ; _ksi_prompt_command'
$ -> declare -p PROMPT_COMMAND 
declare -x PROMPT_COMMAND="history -a; history -n; history -a; history -n; ; _ksi_prompt_command"
bash: PROMPT_COMMAND: line 10: syntax error near unexpected token `;'
bash: PROMPT_COMMAND: line 10: `history -a; history -n; history -a; history -n; ; _ksi_prompt_command'

@kovidgoyal
Copy link
Owner

and? The fix will be in 0.24.3

@securerootd
Copy link

and? The fix will be in 0.24.3

Ah - I misunderstood. I thought the fix was already in 0.24.2 - thanks for clarifying.

@smichel17
Copy link

smichel17 commented Mar 29, 2022

kitty 0.24.4, bash 5.1 (from homebrew)
$ kitty -v
kitty 0.24.4 created by Kovid Goyal
$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-apple-darwin21.1.0)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Unfortunately I've run into a nearly identical bug, when using powerline.

-bash: PROMPT_COMMAND: line 1: syntax error near unexpected token `;'
-bash: PROMPT_COMMAND: line 1: `; builtin declare -F _ksi_prompt_command > /dev/null 2> /dev/null && _ksi_prompt_command'
$ declare -p PROMPT_COMMAND
declare -- PROMPT_COMMAND="_powerline_status_wrapper _powerline_set_prompt
; builtin declare -F _ksi_prompt_command > /dev/null 2> /dev/null && _ksi_prompt_command"

Fortunately, I found a workaround:

  • In kitty.conf, set shell_integration no-rc.
  • In .bashrc, manually init kitty shell integration before powerline setup:
# BEGIN_KITTY_SHELL_INTEGRATION
# This part was not present when reproducing the bug
if test -n "$KITTY_INSTALLATION_DIR" -a -e "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"; then
    source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
fi
# END_KITTY_SHELL_INTEGRATION

# Enable Powerline
# This was also present to produce the bug
if [ -f "$(command -v powerline-daemon)" ]; then
  powerline-daemon -q
  POWERLINE_BASH_CONTINUATION=1
  POWERLINE_BASH_SELECT=1
  # This is terrible. There must be a better way than hard-coding this path.
  . /usr/local/lib/python3.9/site-packages/powerline/bindings/bash/powerline.sh
fi

This produces

$ declare -p PROMPT_COMMAND
declare -a PROMPT_COMMAND=([0]=$'_powerline_status_wrapper _powerline_set_prompt\nbuiltin declare -F _ksi_prompt_command > /dev/null 2> /dev/null && _ksi_prompt_command')

I'm not sure if this is a bug in kitty or powerline, but I'm posting here since I found it as a related issue in my searching; let me know if I should report it to powerline, too (or in its own issue here).

@page-down
Copy link
Contributor

Minimal reproducer

# This works fine
PROMPT_COMMAND=":; "
# Error when more than one space.
PROMPT_COMMAND=":;  "
# or when using newline
PROMPT_COMMAND=":"$'\n' 

# syntax error near unexpected token `;'

Patch

# kitty.bash

# Remove all trailing whitespace chars
PROMPT_COMMAND="${PROMPT_COMMAND%"${PROMPT_COMMAND##*[![:space:]]}"}"
PROMPT_COMMAND="${PROMPT_COMMAND%%;}"

# Or use a newline
PROMPT_COMMAND+=$'\n'"$pc"

@kovidgoyal
Copy link
Owner

5641668

@page-down
Copy link
Contributor

5641668

Does not work when there is more than one whitespace character, is it worth removing?

@kovidgoyal
Copy link
Owner

I have used exglob to handle multiple chars.

@smichel17
Copy link

Thank you both for the better reproduction and exceptionally quick fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants