Bug 1623 - PROMPT_COMMAND replaces ${HOME}x with ~x in PWD
Summary: PROMPT_COMMAND replaces ${HOME}x with ~x in PWD
Status: RESOLVED FIXED
Alias: None
Product: Mageia
Classification: Unclassified
Component: RPM Packages (show other bugs)
Version: Cauldron
Hardware: i586 Linux
Priority: Normal minor
Target Milestone: ---
Assignee: Olivier Blin
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-05 23:21 CEST by Michael LoÃin
Modified: 2016-02-22 00:14 CET (History)
8 users (show)

See Also:
Source RPM: bash-4.2-5.mga1.src.rpm
CVE:
Status comment:


Attachments

Description Michael LoÃin 2011-06-05 23:21:30 CEST
Description of problem:
In /etc/bashrc, PROMPT_COMMAND is set up to replace $HOME with ~ at the
beginning of PWD, but it should only do so if the current directory is actually
$HOME (or a subdirectory thereof).

Version-Release number of selected component (if applicable):
bash-4.1-6mdv2010.1

How reproducible:
always

Steps to Reproduce:
1. have a user with home dir "/home/user"
2. mkdir /home/user2; cd /home/user2
3. terminal title shows "user@host:~2"
Comment 1 Michael LoÃin 2011-06-05 23:24:00 CEST
A possible fix would be a PROMPT_COMMAND like this:

PROMPT_COMMAND='echo -ne "\033]0;${USERNAME:-$USER}@${HOSTNAME%%.*}:"; case "$PWD" in $HOME|$HOME/*) echo -n "${PWD/#$HOME/~}" ;; *) echo -n "$PWD" ;; esac; echo -ne "\007"'

But that's rather ugly...
Comment 2 Ahmad Samir 2011-06-06 00:10:02 CEST
I can't reproduce this issue, after cd /home/user2, it says 'user@hostname: user2'...

Is there anything special about your setup, customisations... etc? which terminal emulator are you using?
Comment 3 Michael LoÃin 2011-06-06 17:55:00 CEST
I use gnome-terminal 3.0.0, but that should not matter (urxvt 9.10 behaves the same).

Do you use the same PROMPT_COMMAND as me? In my /etc/bashrc, it is

PROMPT_COMMAND='echo -ne "\033]0;${USERNAME:-$USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

What does  echo "${PWD/#$HOME/~}"  show in "/home/user2" for you? I get "~2".
Comment 4 Ahmad Samir 2011-06-06 20:19:08 CEST
I think the problem is caused by the user names, 'user' and 'user'2, the share the 'user' part.

[user@localhost ~]$ echo "${PWD/#$HOME/~}"
~
[user@localhost ~]$ cd /home/user2
[user@localhost user2]$ echo "${PWD/#$HOME/~}"
~2
[user@localhost user2]$


[a@localhost ~]$ echo "${PWD/#$HOME/~}"
~
[a@localhost ~]$ cd /home/user2
[a@localhost user2]$ echo "${PWD/#$HOME/~}"
/home/user2
[a@localhost user2]$

The PROMPT_COMMAND here is the default one:
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

it doesn' have ${USERNAME:-$USER} like yours, that may also explain the behaviour you see. (Of course assuming the user names on your box are actually 'user' 'user2' ...).
Comment 5 Michael LoÃin 2011-06-06 20:46:09 CEST
Maybe I should have made that clearer. The user names are indeed the problem, that's why I chose "user" and "user2" as the example.
Samuel Verschelde 2011-10-01 18:00:17 CEST

CC: (none) => stormi
Assignee: bugsquad => shlomif

Comment 6 Marja Van Waes 2012-01-06 19:47:51 CET
pinging. because nothing happened to this report since more than 3 months ago, and it still has the status NEW or REOPENED


@ Shlomi

Please set status to ASSIGNED if you think this bug was assigned correctly. If for work flow reasons you can't do that, then please put OK on the whiteboard instead.

CC: (none) => marja11

Comment 7 Shlomi Fish 2012-01-06 22:09:27 CET
I can reproduce this problem, but only on gnome-terminal - not on KDE's konsole.

Assigning to me.

Regards,

-- Shlomi Fish

Status: NEW => ASSIGNED

Comment 8 Manuel Hiebel 2012-11-05 16:52:45 CET
This message is a reminder that Mageia 1 is nearing its end of life. 
In approximately 25 days from now, Mageia will stop maintaining and issuing 
updates for Mageia 1. At that time this bug will be closed as WONTFIX (EOL) if it 
remains open with a Mageia 'version' of '1'.

Package Maintainer: If you wish for this bug to remain open because you plan to 
fix it in a currently maintained version, simply change the 'version' to a later 
Mageia version prior to Mageia 1's end of life.

Bug Reporter: Thank you for reporting this issue and we are sorry that we may not 
be able to fix it before Mageia 1 is end of life.  If you would still like to see 
this bug fixed and are able to reproduce it against a later version of Mageia, 
you are encouraged to click on "Version" and change it against that version 
of Mageia.

Although we aim to fix as many bugs as possible during every release's lifetime, 
sometimes those efforts are overtaken by events. Often a more recent Mageia 
release includes newer upstream software that fixes bugs or makes them obsolete.

--
Mageia Bugsquad
Comment 9 Shlomi Fish 2012-11-05 17:56:30 CET
Seems to be ok on Cauldron, so resolving.

Status: ASSIGNED => RESOLVED
Resolution: (none) => WORKSFORME

Comment 10 Michael LoÃin 2012-11-25 11:36:28 CET
Checked with bash-4.2-37.2.mga3 for Mageia 3 Alpha; the problem is still there.

Please note that only PROMPT_COMMAND is affected (i.e. the window and tab title), but not PS1 (the bash prompt), since that uses "\W" to correctly replace "$HOME" with "~".

Status: RESOLVED => REOPENED
Version: 1 => Cauldron
Resolution: WORKSFORME => (none)

Comment 11 Shlomi Fish 2012-11-26 10:41:01 CET
(In reply to comment #10)
> Checked with bash-4.2-37.2.mga3 for Mageia 3 Alpha; the problem is still there.
> 
> Please note that only PROMPT_COMMAND is affected (i.e. the window and tab
> title), but not PS1 (the bash prompt), since that uses "\W" to correctly
> replace "$HOME" with "~".

Can you write a test case script? I find it hard to reproduce.

Regards,

-- Shlomi Fish
Comment 12 Michael LoÃin 2012-11-26 17:27:00 CET
[root@localhost ~]# useradd user
[root@localhost ~]# useradd user2
[root@localhost ~]# su - user
[user@localhost ~]$ cd ~user2
[user@localhost user2]$ 

At that time, the LXTerminal (maybe others as well) show "user@localhost:~2" in the title, while it should be "user@localhost:/home/user2".

Please note that this only affects user names where one is the prefix of the other, since then $HOME of "user" (= /home/user) is a prefix of $HOME of "user2" (= /home/user2), so that the path "/home/user2" gets transformed to "~2".

(See comment #1 for a possible ugly fix.)
Comment 13 Shlomi Fish 2012-12-07 20:59:21 CET
I can also do this:

<<<<
PROMPT_COMMAND='history -a; echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:$(perl -le '\''use Cwd; print Cwd::getcwd() =~ s#\A\Q$ENV{HOME}\E(?=/|\z)#~#r'\'');" echo -ne "\007"'
>>>>

Maybe it can be done using sed, but I dislike sed, and being Mageia we can always rely on perl to be there. But if someone can suggest a sed (or maybe awk) solution - I'm all ears.

Regards,

-- Shlomi Fish
Comment 14 Shlomi Fish 2012-12-08 20:40:00 CET
This should be fixed on Mageia Cauldron. Please test. I think the bug is not critical enough to warrant an update on Mageia 2.
Comment 15 Florian Hubold 2015-02-04 22:54:24 CET
FWIW, the perl call should be replaced. For every prompt call, it's overkill to call an external program just to display pwd and shorten to ~ when in $HOME/.

See also https://bugs.mageia.org/show_bug.cgi?id=3723#c47
Not sure why we don't simply use the bash prompt strings \w or \W to either show full path or only basename of current directory or ~ when pwd=$HOME, this works fine here for me.

I'll see what I can dig up, there are several more readable solutions doing this either in bash or posix-compatible.

CC: (none) => doktor5000

Comment 16 Olivier Blin 2015-09-06 02:56:10 CEST
I agree, using perl here for each new shell prompt line is overkill.
Do we still need this PROMPT_COMMAND and PS1 hacks in /etc/bashrc?

CC: (none) => mageia, mageia, thierry.vignaud, tmb

Comment 17 Olivier Blin 2016-02-22 00:02:09 CET
(In reply to Florian Hubold from comment #15)
> Not sure why we don't simply use the bash prompt strings \w or \W to either
> show full path or only basename of current directory or ~ when pwd=$HOME,
> this works fine here for me.

We can not use \w or \W in PROMPT_COMMAND, since it contains a command to be executed, and does not expand special characters like PS1.

I'll replace the perl command with something based on this:
http://stackoverflow.com/questions/10036255/is-there-a-good-way-to-replace-home-directory-with-tilde-in-bash/10037257#10037257

And the xterm/screen case can be factorized a bit.

This gives:

    i='${USER}@${HOSTNAME%%.*}:$([[ "${PWD}" =~ ^"${HOME}"(/|$) ]] && echo "~${PWD#${HOME}}" || echo "${PWD}")'
    case $TERM in
        xterm*)
            PROMPT_COMMAND='echo -ne "\033]0;'${i}'\007"'
            ;;
        screen)
            PROMPT_COMMAND='echo -ne "\033_'${i}'\033\\"'
            ;;
        *)
            ;;
    esac
    unset i

CC: (none) => shlomif
Assignee: shlomif => mageia

Comment 18 Olivier Blin 2016-02-22 00:14:46 CET
Fixed in bash-4.3-42.5.mga6

Status: REOPENED => RESOLVED
Resolution: (none) => FIXED


Note You need to log in before you can comment on or make changes to this bug.