linux - Is it possible to have environment variables in the path of the working directory : PS1?

05
2013-12
  • mthpvg

    I am on Lubuntu and I am using bash. My PS1 (in .bashrc) is :

    PS1="\w> "
    

    I like it because I need to paste the working directory all the time. The problem is that the path is always very long and since I use terminator I only have half of my screen's width available to display it... it is ugly and annoying. My command prompt looks like that :

    /this/is/a/very/long/path/that/i/want/to/make/shorter >
    

    I'd like to set in my environment variables :

    $tiavl=/this/is/a/very/long
    

    And then I'll get :

    $tiavl/path/that/i/want/to/make/shorter >
    

    The goal is to have something shorter in the command prompt but I still want to be able to copy paste it and do :

    cd $tiavl/path/that/i/want/to/make/shorter
    

    It is a bit like with $HOME :

    ~/path/that/i/want/to/make/shorter  >
    

    I know where I am and I can copy paste the ~.

    Thanks.

  • Answers
  • Mat

    You can do this with a little helper function, as below (use /home as an example prefix path):

    ~ > pwd
    /home/me
    ~ > tiavl=/home
    ~ > prompt_path () { echo ${1/#$tiavl/\$tiavl}; }
    ~ > export PS1="\$(prompt_path \w) > "
    $tiavl/me > 
    

    This uses a simple string manipulation function (see here for lots of examples) in the function to replace the initial part of the path with a literal $tiavl if it matches.

    Here's a demo of how to update that function for several paths.

    #! /bin/sh
    
    path1=/home
    path2=/usr
    path3=/var
    
    prompt_path() {
        local path
        path="${1/#$path1/\$path1}"
        path="${path/#$path2/\$path2}"
        path="${path/#$path3/\$path3}"
        echo "$path"
    }
    
    prompt_path $HOME
    prompt_path /usr/local
    prompt_path /var/tmp
    
  • samir

    If you copy paste in the terminal, you can just use $PWD environment variable which will always show you the working directory.

    echo $PWD
    

    shows the working directory.

  • mthpvg

    So basically you make a bash script :

    prompt_path
    

    which contains :

    #! /bin/sh
    path="${1/#$path1/\$path1}"
    path="${path/#$path2/\$path2}"
    path="${path/#$path3/\$path3}"
    echo "$path"
    

    You place it in a folder like :

    ~/.local/bin
    

    You give x right to your script :

    chmod u+x prompt_path
    

    In your ~/.bashrc :

    1 - you change PS1 to :

    PS1="\$(prompt_path \w) > "
    

    2 - you add those lines :

    export path1=/home
    export path2=/usr
    export path3=/var
    

    3 - you indicate so that you can call your script from anywhere:

    export PATH=~/.local/bin:$PATH
    

    Finally you source your .bashrc :

    . ~/.bashrc
    

  • Related Question

    bash - How do I fix my prompt in emacs shell-mode?
  • Jay Conrod

    I'm doing some programming on a colleague's machine. He has a version of emacs (23.1.1) I haven't used before. My problem is that when I go to shell-mode, my bash prompt looks like this:

    ^[]0;jay@socrates:~^G[jay@socrates]$
    

    I have PS1 set to '[\u@\h]\$ ' in my .bashrc. It's supposed to look like this:

    [jay@socrates]$
    

    This is how it looks in a regular shell. I've also checked that PS1 is set to the correct value in the emacs shell, so now I'm out of ideas. How can I get my prompt to look the way I want?

    I've seen some suggestions to use term or eshell instead of shell. term has the same prompt problem as above, and eshell completely ignores my PS1 so that doesn't really help.


  • Related Answers
  • Peter

    Your shell is trying to set the XTerm's (or other console) title/header. There are a number of ways this might be being done.

    First make sure the PS1 is really what you think it is

    echo $PS1 | less -E
    

    That will tell you if there are control characters in the prompt, less will make them look funny. Assuming your prompt is exactly as you say, then it is probably the PROMPT_COMMAND environment variable. You can look at that the same way...

    echo $PROMPT_COMMAND | less -E
    

    If the prompt command is the problem, then you can just unset it. In either case, these variables are being set up somewhere, and should not be set to update the XTerm header, if you're not in an XTerm!

    You can look in the "standard" bashrc (/etc/bashrc). You should see code that checks for an iteractive shell (is PS1 set), and then checks for xterm (looking at $TERM variable), and does something different there than for other terminal types.

    I suspect somewhere in the bash initialization is a hard-coded setup, which should only be done on xterm-compatible console programs. Read man bash to find a total list of files you can look for and through.

    If all of the above fails, try

    printenv | less
    

    And see if you can find something suspicious in there, and then track down where it is being set. Post it here if you can't work it out.

  • SG1

    I came across exact same problem and it is due to PROMPT_COMMAND. I like the xterm title. So I added following line in ~/.emacs_bash

    export PROMPT_COMMAND=""
    
  • luapyad

    It may be something to do with the terminal type of the emacs shell vs your regular shell. Check what the TERM environment variable is set to in both shells. If they are different then you should be able to change the emacs shell terminal type or use a different PS1 definition for the emacs terminal type in .bashrc.

    You could also try M-x ansi-term (and ansi-color-for-comint-mode-on if you have colour prompts).

  • contact us

    This was answered very well on the stackoverflow site. by Daniel Poe.

    Here's his answer.

    You can use AnsiTerm which does support colors or you can enable AnsiColor for the normal shell:

    (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
    (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)