Show only current and parent directory in bash prompt

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite
3












I'm new to bash and would like my prompt to show something that in tcsh was trivial, yet after a good google search I still cannot do.



I would like my prompt to include only the current and parent directories like this:



/parent/currentdir $


In tcsh this is achieved by:



set prompt = "%C2 %"


However in bash so far I have only found that I have to parse pwd to obtain the same output.



Isn't there a simpler way, like doing:



export PS1="$(some_command) $" 









share|improve this question



















  • 1




    In theory, PS1='w $'; PROMPT_DIRTRIM=2 should give you the bash equivalent, but that doesn't work properly on my system.
    – Mikel
    Jul 18 '15 at 23:15











  • It doesn't work at all on mine: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).
    – twalbaum
    Aug 7 '15 at 23:19










  • PROMPT_DIRTRIM was introduced in Bash 4. I just tested on Ubuntu 16.04 under WSL and it worked great!
    – Mike Branski
    Dec 8 '17 at 18:08














up vote
10
down vote

favorite
3












I'm new to bash and would like my prompt to show something that in tcsh was trivial, yet after a good google search I still cannot do.



I would like my prompt to include only the current and parent directories like this:



/parent/currentdir $


In tcsh this is achieved by:



set prompt = "%C2 %"


However in bash so far I have only found that I have to parse pwd to obtain the same output.



Isn't there a simpler way, like doing:



export PS1="$(some_command) $" 









share|improve this question



















  • 1




    In theory, PS1='w $'; PROMPT_DIRTRIM=2 should give you the bash equivalent, but that doesn't work properly on my system.
    – Mikel
    Jul 18 '15 at 23:15











  • It doesn't work at all on mine: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).
    – twalbaum
    Aug 7 '15 at 23:19










  • PROMPT_DIRTRIM was introduced in Bash 4. I just tested on Ubuntu 16.04 under WSL and it worked great!
    – Mike Branski
    Dec 8 '17 at 18:08












up vote
10
down vote

favorite
3









up vote
10
down vote

favorite
3






3





I'm new to bash and would like my prompt to show something that in tcsh was trivial, yet after a good google search I still cannot do.



I would like my prompt to include only the current and parent directories like this:



/parent/currentdir $


In tcsh this is achieved by:



set prompt = "%C2 %"


However in bash so far I have only found that I have to parse pwd to obtain the same output.



Isn't there a simpler way, like doing:



export PS1="$(some_command) $" 









share|improve this question















I'm new to bash and would like my prompt to show something that in tcsh was trivial, yet after a good google search I still cannot do.



I would like my prompt to include only the current and parent directories like this:



/parent/currentdir $


In tcsh this is achieved by:



set prompt = "%C2 %"


However in bash so far I have only found that I have to parse pwd to obtain the same output.



Isn't there a simpler way, like doing:



export PS1="$(some_command) $" 






bash prompt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 18 '15 at 23:06









Mikel

38.8k1098125




38.8k1098125










asked Jul 18 '15 at 20:11









twalbaum

173117




173117







  • 1




    In theory, PS1='w $'; PROMPT_DIRTRIM=2 should give you the bash equivalent, but that doesn't work properly on my system.
    – Mikel
    Jul 18 '15 at 23:15











  • It doesn't work at all on mine: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).
    – twalbaum
    Aug 7 '15 at 23:19










  • PROMPT_DIRTRIM was introduced in Bash 4. I just tested on Ubuntu 16.04 under WSL and it worked great!
    – Mike Branski
    Dec 8 '17 at 18:08












  • 1




    In theory, PS1='w $'; PROMPT_DIRTRIM=2 should give you the bash equivalent, but that doesn't work properly on my system.
    – Mikel
    Jul 18 '15 at 23:15











  • It doesn't work at all on mine: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).
    – twalbaum
    Aug 7 '15 at 23:19










  • PROMPT_DIRTRIM was introduced in Bash 4. I just tested on Ubuntu 16.04 under WSL and it worked great!
    – Mike Branski
    Dec 8 '17 at 18:08







1




1




In theory, PS1='w $'; PROMPT_DIRTRIM=2 should give you the bash equivalent, but that doesn't work properly on my system.
– Mikel
Jul 18 '15 at 23:15





In theory, PS1='w $'; PROMPT_DIRTRIM=2 should give you the bash equivalent, but that doesn't work properly on my system.
– Mikel
Jul 18 '15 at 23:15













It doesn't work at all on mine: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).
– twalbaum
Aug 7 '15 at 23:19




It doesn't work at all on mine: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).
– twalbaum
Aug 7 '15 at 23:19












PROMPT_DIRTRIM was introduced in Bash 4. I just tested on Ubuntu 16.04 under WSL and it worked great!
– Mike Branski
Dec 8 '17 at 18:08




PROMPT_DIRTRIM was introduced in Bash 4. I just tested on Ubuntu 16.04 under WSL and it worked great!
– Mike Branski
Dec 8 '17 at 18:08










5 Answers
5






active

oldest

votes

















up vote
13
down vote



accepted










Bash's prompt control features are rather static. If you want more control, you can include variables in your prompt; make sure you haven't turned off the promptvars option.



PS1='$PWD#"$PWD%/*/*/" $ '


Note the single quotes: the variable expansions must happen at the time the prompt is displayed, not at the time the PS1 variable is defined.



If you want more control over what is displayed, you can use command substitutions. For example, the snippet above loses the ~ abbreviation for the home directory.



PS1='$(case $PWD in
$HOME) HPWD="~";;
$HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
$HOME/*) HPWD="~/$PWD##*/";;
/*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
*) HPWD="$PWD";;
esac; printf %s "$HPWD") $ '


This code is rather cumbersome, so instead of sticking it into the PS1 variable, you can use the PROMPT_COMMAND variable to run code to set HPWD and then use that in your prompt.



PROMPT_COMMAND='case $PWD in
$HOME) HPWD="~";;
$HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
$HOME/*) HPWD="~/$PWD##*/";;
/*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
*) HPWD="$PWD";;
esac'
PS1='$HPWD $'


Since the shortened prompt only changed on a directory change, you don't need to recalculate it each time a prompt is displayed. Bash doesn't provide a hook that runs on a current directory change, but you can simulate it by overriding cd and its cousins.



cd () builtin cd "$@" && chpwd; 
pushd () builtin pushd "$@" && chpwd;
popd () builtin popd "$@" && chpwd;
chpwd ()
case $PWD in
$HOME) HPWD="~";;
$HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
$HOME/*) HPWD="~/$PWD##*/";;
/*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
*) HPWD="$PWD";;
esac

PS1='$HPWD $'


Note that you don't need to, and should not, export PS1, since it's a shell setting, not an environment variable. A bash PS1 setting wouldn't be understood by other shells.



P.S. If you want a nice interactive shell experience, switch to zsh, where all of these (prompt % expansions largely encompassing tcsh's, chpwd, etc.) are native features.



PS1='%2~ %# '





share|improve this answer


















  • 1




    This should have been the accepted and most upvoted answer.
    – LarsH
    Jun 21 '16 at 12:28

















up vote
2
down vote













The syntax for obtaining the parent and current directories is taken from Munai's answer.



However, as noted by Gilles, that code only shows the current directory at the time .bashrc is loaded, but it won't change as you navigate the system to other folders.



Having this in your .bashrc file makes the prompt automatically updated to your current directory:



prompt_command () 
PS1='$(basename $(dirname "$PWD"))/$(basename "$PWD") $ '

PROMPT_COMMAND=prompt_command





share|improve this answer


















  • 1




    Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
    – LarsH
    Jun 21 '16 at 12:27







  • 2




    Also, this command fails when $PWD contains a space.
    – LarsH
    Jun 21 '16 at 12:37






  • 1




    Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
    – twalbaum
    Jul 8 '16 at 20:36











  • Any ideas for cleaning up the output when navigating to /? As written, you get /// $
    – ezrock
    Nov 10 '17 at 18:14


















up vote
0
down vote













Prompt string can be easily changed in bash by editing the shell variable PS1. It stands for Prompt String 1. More info here.



For now fire up your bash shell.



vi ~/.bashrc



Append the PS1 definition in the file



`export PS1="$(basename $(dirname $PWD))/$(basename $PWD)"`


More tutorials here and here, to help you tweak it even more.






share|improve this answer


















  • 1




    I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
    – twalbaum
    Jul 18 '15 at 20:50










  • @twalbaum edited my answer
    – Munai Das Udasin
    Jul 18 '15 at 20:59











  • Perfect! Thanks
    – twalbaum
    Jul 18 '15 at 21:04










  • glad to help @twalbaum :)
    – Munai Das Udasin
    Jul 18 '15 at 21:04






  • 1




    This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
    – Gilles
    Jul 19 '15 at 21:16

















up vote
0
down vote













May be simpler one with "~" for Home directory.



function PWDN cut -d "/" -f1 -f2 





share|improve this answer



























    up vote
    0
    down vote













    export PS1='[u@h $(basename $(dirname $PWD))/$(basename $PWD)]$ '
    export PROMPT_COMMAND='echo -ne "33]0;$(basename $(dirname $PWD))/$(basename $PWD)07"'





    share|improve this answer




















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f216953%2fshow-only-current-and-parent-directory-in-bash-prompt%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote



      accepted










      Bash's prompt control features are rather static. If you want more control, you can include variables in your prompt; make sure you haven't turned off the promptvars option.



      PS1='$PWD#"$PWD%/*/*/" $ '


      Note the single quotes: the variable expansions must happen at the time the prompt is displayed, not at the time the PS1 variable is defined.



      If you want more control over what is displayed, you can use command substitutions. For example, the snippet above loses the ~ abbreviation for the home directory.



      PS1='$(case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac; printf %s "$HPWD") $ '


      This code is rather cumbersome, so instead of sticking it into the PS1 variable, you can use the PROMPT_COMMAND variable to run code to set HPWD and then use that in your prompt.



      PROMPT_COMMAND='case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac'
      PS1='$HPWD $'


      Since the shortened prompt only changed on a directory change, you don't need to recalculate it each time a prompt is displayed. Bash doesn't provide a hook that runs on a current directory change, but you can simulate it by overriding cd and its cousins.



      cd () builtin cd "$@" && chpwd; 
      pushd () builtin pushd "$@" && chpwd;
      popd () builtin popd "$@" && chpwd;
      chpwd ()
      case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac

      PS1='$HPWD $'


      Note that you don't need to, and should not, export PS1, since it's a shell setting, not an environment variable. A bash PS1 setting wouldn't be understood by other shells.



      P.S. If you want a nice interactive shell experience, switch to zsh, where all of these (prompt % expansions largely encompassing tcsh's, chpwd, etc.) are native features.



      PS1='%2~ %# '





      share|improve this answer


















      • 1




        This should have been the accepted and most upvoted answer.
        – LarsH
        Jun 21 '16 at 12:28














      up vote
      13
      down vote



      accepted










      Bash's prompt control features are rather static. If you want more control, you can include variables in your prompt; make sure you haven't turned off the promptvars option.



      PS1='$PWD#"$PWD%/*/*/" $ '


      Note the single quotes: the variable expansions must happen at the time the prompt is displayed, not at the time the PS1 variable is defined.



      If you want more control over what is displayed, you can use command substitutions. For example, the snippet above loses the ~ abbreviation for the home directory.



      PS1='$(case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac; printf %s "$HPWD") $ '


      This code is rather cumbersome, so instead of sticking it into the PS1 variable, you can use the PROMPT_COMMAND variable to run code to set HPWD and then use that in your prompt.



      PROMPT_COMMAND='case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac'
      PS1='$HPWD $'


      Since the shortened prompt only changed on a directory change, you don't need to recalculate it each time a prompt is displayed. Bash doesn't provide a hook that runs on a current directory change, but you can simulate it by overriding cd and its cousins.



      cd () builtin cd "$@" && chpwd; 
      pushd () builtin pushd "$@" && chpwd;
      popd () builtin popd "$@" && chpwd;
      chpwd ()
      case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac

      PS1='$HPWD $'


      Note that you don't need to, and should not, export PS1, since it's a shell setting, not an environment variable. A bash PS1 setting wouldn't be understood by other shells.



      P.S. If you want a nice interactive shell experience, switch to zsh, where all of these (prompt % expansions largely encompassing tcsh's, chpwd, etc.) are native features.



      PS1='%2~ %# '





      share|improve this answer


















      • 1




        This should have been the accepted and most upvoted answer.
        – LarsH
        Jun 21 '16 at 12:28












      up vote
      13
      down vote



      accepted







      up vote
      13
      down vote



      accepted






      Bash's prompt control features are rather static. If you want more control, you can include variables in your prompt; make sure you haven't turned off the promptvars option.



      PS1='$PWD#"$PWD%/*/*/" $ '


      Note the single quotes: the variable expansions must happen at the time the prompt is displayed, not at the time the PS1 variable is defined.



      If you want more control over what is displayed, you can use command substitutions. For example, the snippet above loses the ~ abbreviation for the home directory.



      PS1='$(case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac; printf %s "$HPWD") $ '


      This code is rather cumbersome, so instead of sticking it into the PS1 variable, you can use the PROMPT_COMMAND variable to run code to set HPWD and then use that in your prompt.



      PROMPT_COMMAND='case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac'
      PS1='$HPWD $'


      Since the shortened prompt only changed on a directory change, you don't need to recalculate it each time a prompt is displayed. Bash doesn't provide a hook that runs on a current directory change, but you can simulate it by overriding cd and its cousins.



      cd () builtin cd "$@" && chpwd; 
      pushd () builtin pushd "$@" && chpwd;
      popd () builtin popd "$@" && chpwd;
      chpwd ()
      case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac

      PS1='$HPWD $'


      Note that you don't need to, and should not, export PS1, since it's a shell setting, not an environment variable. A bash PS1 setting wouldn't be understood by other shells.



      P.S. If you want a nice interactive shell experience, switch to zsh, where all of these (prompt % expansions largely encompassing tcsh's, chpwd, etc.) are native features.



      PS1='%2~ %# '





      share|improve this answer














      Bash's prompt control features are rather static. If you want more control, you can include variables in your prompt; make sure you haven't turned off the promptvars option.



      PS1='$PWD#"$PWD%/*/*/" $ '


      Note the single quotes: the variable expansions must happen at the time the prompt is displayed, not at the time the PS1 variable is defined.



      If you want more control over what is displayed, you can use command substitutions. For example, the snippet above loses the ~ abbreviation for the home directory.



      PS1='$(case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac; printf %s "$HPWD") $ '


      This code is rather cumbersome, so instead of sticking it into the PS1 variable, you can use the PROMPT_COMMAND variable to run code to set HPWD and then use that in your prompt.



      PROMPT_COMMAND='case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac'
      PS1='$HPWD $'


      Since the shortened prompt only changed on a directory change, you don't need to recalculate it each time a prompt is displayed. Bash doesn't provide a hook that runs on a current directory change, but you can simulate it by overriding cd and its cousins.



      cd () builtin cd "$@" && chpwd; 
      pushd () builtin pushd "$@" && chpwd;
      popd () builtin popd "$@" && chpwd;
      chpwd ()
      case $PWD in
      $HOME) HPWD="~";;
      $HOME/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      $HOME/*) HPWD="~/$PWD##*/";;
      /*/*/*) HPWD="$PWD#"$PWD%/*/*/"";;
      *) HPWD="$PWD";;
      esac

      PS1='$HPWD $'


      Note that you don't need to, and should not, export PS1, since it's a shell setting, not an environment variable. A bash PS1 setting wouldn't be understood by other shells.



      P.S. If you want a nice interactive shell experience, switch to zsh, where all of these (prompt % expansions largely encompassing tcsh's, chpwd, etc.) are native features.



      PS1='%2~ %# '






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 13 '17 at 12:36









      Community

      1




      1










      answered Jul 19 '15 at 23:28









      Gilles

      525k12710491578




      525k12710491578







      • 1




        This should have been the accepted and most upvoted answer.
        – LarsH
        Jun 21 '16 at 12:28












      • 1




        This should have been the accepted and most upvoted answer.
        – LarsH
        Jun 21 '16 at 12:28







      1




      1




      This should have been the accepted and most upvoted answer.
      – LarsH
      Jun 21 '16 at 12:28




      This should have been the accepted and most upvoted answer.
      – LarsH
      Jun 21 '16 at 12:28












      up vote
      2
      down vote













      The syntax for obtaining the parent and current directories is taken from Munai's answer.



      However, as noted by Gilles, that code only shows the current directory at the time .bashrc is loaded, but it won't change as you navigate the system to other folders.



      Having this in your .bashrc file makes the prompt automatically updated to your current directory:



      prompt_command () 
      PS1='$(basename $(dirname "$PWD"))/$(basename "$PWD") $ '

      PROMPT_COMMAND=prompt_command





      share|improve this answer


















      • 1




        Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
        – LarsH
        Jun 21 '16 at 12:27







      • 2




        Also, this command fails when $PWD contains a space.
        – LarsH
        Jun 21 '16 at 12:37






      • 1




        Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
        – twalbaum
        Jul 8 '16 at 20:36











      • Any ideas for cleaning up the output when navigating to /? As written, you get /// $
        – ezrock
        Nov 10 '17 at 18:14















      up vote
      2
      down vote













      The syntax for obtaining the parent and current directories is taken from Munai's answer.



      However, as noted by Gilles, that code only shows the current directory at the time .bashrc is loaded, but it won't change as you navigate the system to other folders.



      Having this in your .bashrc file makes the prompt automatically updated to your current directory:



      prompt_command () 
      PS1='$(basename $(dirname "$PWD"))/$(basename "$PWD") $ '

      PROMPT_COMMAND=prompt_command





      share|improve this answer


















      • 1




        Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
        – LarsH
        Jun 21 '16 at 12:27







      • 2




        Also, this command fails when $PWD contains a space.
        – LarsH
        Jun 21 '16 at 12:37






      • 1




        Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
        – twalbaum
        Jul 8 '16 at 20:36











      • Any ideas for cleaning up the output when navigating to /? As written, you get /// $
        – ezrock
        Nov 10 '17 at 18:14













      up vote
      2
      down vote










      up vote
      2
      down vote









      The syntax for obtaining the parent and current directories is taken from Munai's answer.



      However, as noted by Gilles, that code only shows the current directory at the time .bashrc is loaded, but it won't change as you navigate the system to other folders.



      Having this in your .bashrc file makes the prompt automatically updated to your current directory:



      prompt_command () 
      PS1='$(basename $(dirname "$PWD"))/$(basename "$PWD") $ '

      PROMPT_COMMAND=prompt_command





      share|improve this answer














      The syntax for obtaining the parent and current directories is taken from Munai's answer.



      However, as noted by Gilles, that code only shows the current directory at the time .bashrc is loaded, but it won't change as you navigate the system to other folders.



      Having this in your .bashrc file makes the prompt automatically updated to your current directory:



      prompt_command () 
      PS1='$(basename $(dirname "$PWD"))/$(basename "$PWD") $ '

      PROMPT_COMMAND=prompt_command






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 13 '17 at 12:36









      Community

      1




      1










      answered Jul 19 '15 at 22:01









      twalbaum

      173117




      173117







      • 1




        Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
        – LarsH
        Jun 21 '16 at 12:27







      • 2




        Also, this command fails when $PWD contains a space.
        – LarsH
        Jun 21 '16 at 12:37






      • 1




        Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
        – twalbaum
        Jul 8 '16 at 20:36











      • Any ideas for cleaning up the output when navigating to /? As written, you get /// $
        – ezrock
        Nov 10 '17 at 18:14













      • 1




        Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
        – LarsH
        Jun 21 '16 at 12:27







      • 2




        Also, this command fails when $PWD contains a space.
        – LarsH
        Jun 21 '16 at 12:37






      • 1




        Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
        – twalbaum
        Jul 8 '16 at 20:36











      • Any ideas for cleaning up the output when navigating to /? As written, you get /// $
        – ezrock
        Nov 10 '17 at 18:14








      1




      1




      Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
      – LarsH
      Jun 21 '16 at 12:27





      Is defining prompt_command() supposed to do something, on its own? It doesn't for me. Did you mean to also use prompt_command() in redefinitions of cd & co, like Gilles did? Or did you mean PROMPT_COMMAND='...'?
      – LarsH
      Jun 21 '16 at 12:27





      2




      2




      Also, this command fails when $PWD contains a space.
      – LarsH
      Jun 21 '16 at 12:37




      Also, this command fails when $PWD contains a space.
      – LarsH
      Jun 21 '16 at 12:37




      1




      1




      Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
      – twalbaum
      Jul 8 '16 at 20:36





      Thank you for bringing me back to this. Quoted the $PWDs to allow for spaces and included the final command. My current prompt is more complex than this, therefore the need for the prompt_command definition. Otherwise, PS1=... works on its on.
      – twalbaum
      Jul 8 '16 at 20:36













      Any ideas for cleaning up the output when navigating to /? As written, you get /// $
      – ezrock
      Nov 10 '17 at 18:14





      Any ideas for cleaning up the output when navigating to /? As written, you get /// $
      – ezrock
      Nov 10 '17 at 18:14











      up vote
      0
      down vote













      Prompt string can be easily changed in bash by editing the shell variable PS1. It stands for Prompt String 1. More info here.



      For now fire up your bash shell.



      vi ~/.bashrc



      Append the PS1 definition in the file



      `export PS1="$(basename $(dirname $PWD))/$(basename $PWD)"`


      More tutorials here and here, to help you tweak it even more.






      share|improve this answer


















      • 1




        I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
        – twalbaum
        Jul 18 '15 at 20:50










      • @twalbaum edited my answer
        – Munai Das Udasin
        Jul 18 '15 at 20:59











      • Perfect! Thanks
        – twalbaum
        Jul 18 '15 at 21:04










      • glad to help @twalbaum :)
        – Munai Das Udasin
        Jul 18 '15 at 21:04






      • 1




        This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
        – Gilles
        Jul 19 '15 at 21:16














      up vote
      0
      down vote













      Prompt string can be easily changed in bash by editing the shell variable PS1. It stands for Prompt String 1. More info here.



      For now fire up your bash shell.



      vi ~/.bashrc



      Append the PS1 definition in the file



      `export PS1="$(basename $(dirname $PWD))/$(basename $PWD)"`


      More tutorials here and here, to help you tweak it even more.






      share|improve this answer


















      • 1




        I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
        – twalbaum
        Jul 18 '15 at 20:50










      • @twalbaum edited my answer
        – Munai Das Udasin
        Jul 18 '15 at 20:59











      • Perfect! Thanks
        – twalbaum
        Jul 18 '15 at 21:04










      • glad to help @twalbaum :)
        – Munai Das Udasin
        Jul 18 '15 at 21:04






      • 1




        This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
        – Gilles
        Jul 19 '15 at 21:16












      up vote
      0
      down vote










      up vote
      0
      down vote









      Prompt string can be easily changed in bash by editing the shell variable PS1. It stands for Prompt String 1. More info here.



      For now fire up your bash shell.



      vi ~/.bashrc



      Append the PS1 definition in the file



      `export PS1="$(basename $(dirname $PWD))/$(basename $PWD)"`


      More tutorials here and here, to help you tweak it even more.






      share|improve this answer














      Prompt string can be easily changed in bash by editing the shell variable PS1. It stands for Prompt String 1. More info here.



      For now fire up your bash shell.



      vi ~/.bashrc



      Append the PS1 definition in the file



      `export PS1="$(basename $(dirname $PWD))/$(basename $PWD)"`


      More tutorials here and here, to help you tweak it even more.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 18 '15 at 23:05









      Mikel

      38.8k1098125




      38.8k1098125










      answered Jul 18 '15 at 20:44









      Munai Das Udasin

      3691315




      3691315







      • 1




        I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
        – twalbaum
        Jul 18 '15 at 20:50










      • @twalbaum edited my answer
        – Munai Das Udasin
        Jul 18 '15 at 20:59











      • Perfect! Thanks
        – twalbaum
        Jul 18 '15 at 21:04










      • glad to help @twalbaum :)
        – Munai Das Udasin
        Jul 18 '15 at 21:04






      • 1




        This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
        – Gilles
        Jul 19 '15 at 21:16












      • 1




        I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
        – twalbaum
        Jul 18 '15 at 20:50










      • @twalbaum edited my answer
        – Munai Das Udasin
        Jul 18 '15 at 20:59











      • Perfect! Thanks
        – twalbaum
        Jul 18 '15 at 21:04










      • glad to help @twalbaum :)
        – Munai Das Udasin
        Jul 18 '15 at 21:04






      • 1




        This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
        – Gilles
        Jul 19 '15 at 21:16







      1




      1




      I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
      – twalbaum
      Jul 18 '15 at 20:50




      I understand how to modify the prompt. My question is how to get only the current and parent directories, not the whole path.
      – twalbaum
      Jul 18 '15 at 20:50












      @twalbaum edited my answer
      – Munai Das Udasin
      Jul 18 '15 at 20:59





      @twalbaum edited my answer
      – Munai Das Udasin
      Jul 18 '15 at 20:59













      Perfect! Thanks
      – twalbaum
      Jul 18 '15 at 21:04




      Perfect! Thanks
      – twalbaum
      Jul 18 '15 at 21:04












      glad to help @twalbaum :)
      – Munai Das Udasin
      Jul 18 '15 at 21:04




      glad to help @twalbaum :)
      – Munai Das Udasin
      Jul 18 '15 at 21:04




      1




      1




      This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
      – Gilles
      Jul 19 '15 at 21:16




      This sets the prompt to the current directory at the time .bashrc is loaded, the prompt won't change if you cd into another directory! @twalbaum
      – Gilles
      Jul 19 '15 at 21:16










      up vote
      0
      down vote













      May be simpler one with "~" for Home directory.



      function PWDN cut -d "/" -f1 -f2 





      share|improve this answer
























        up vote
        0
        down vote













        May be simpler one with "~" for Home directory.



        function PWDN cut -d "/" -f1 -f2 





        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          May be simpler one with "~" for Home directory.



          function PWDN cut -d "/" -f1 -f2 





          share|improve this answer












          May be simpler one with "~" for Home directory.



          function PWDN cut -d "/" -f1 -f2 






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 20 '17 at 0:27









          Baskar

          1011




          1011




















              up vote
              0
              down vote













              export PS1='[u@h $(basename $(dirname $PWD))/$(basename $PWD)]$ '
              export PROMPT_COMMAND='echo -ne "33]0;$(basename $(dirname $PWD))/$(basename $PWD)07"'





              share|improve this answer
























                up vote
                0
                down vote













                export PS1='[u@h $(basename $(dirname $PWD))/$(basename $PWD)]$ '
                export PROMPT_COMMAND='echo -ne "33]0;$(basename $(dirname $PWD))/$(basename $PWD)07"'





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  export PS1='[u@h $(basename $(dirname $PWD))/$(basename $PWD)]$ '
                  export PROMPT_COMMAND='echo -ne "33]0;$(basename $(dirname $PWD))/$(basename $PWD)07"'





                  share|improve this answer












                  export PS1='[u@h $(basename $(dirname $PWD))/$(basename $PWD)]$ '
                  export PROMPT_COMMAND='echo -ne "33]0;$(basename $(dirname $PWD))/$(basename $PWD)07"'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 14 '17 at 6:44









                  Marius Andreiana

                  1062




                  1062



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f216953%2fshow-only-current-and-parent-directory-in-bash-prompt%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay