Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:05:46+00:00 2026-05-29T06:05:46+00:00

In my .bashrc , I got this: PS1=[\w $] And every time when I

  • 0

In my .bashrc, I got this:

PS1="[\w $]"

And every time when I cd to a dir with a deep level, the shell prompt almost takes up the whole line, (terminal size: 80*24), like:

[/level_a_dir/level_b_dir/level_c_dir/level_d_dir/level_e_dir $]

Question

I want to cut the prompt short if the pwd is longer than 20 chars, just keep the last dir, like:

[.../level_e_dir $]
#[/level_a_dir/level_b_dir/level_c_dir/level_d_dir] is replaced with ...

How to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:05:46+00:00Added an answer on May 29, 2026 at 6:05 am

    I have done it in the following way.
    First you have to create a shell script, truncate.sh:

    #!/bin/bash
    MAXLEN=20
    REPLACEMENT="..."
    
    # replace /home/user by ~
    TPWD=$(echo ${PWD} | sed 's#'${HOME}'#~#;')
    
    # truncate
    if [ ${#TPWD} -gt ${MAXLEN} ] ; then
      PWDOFFSET=$(( ${#TPWD} - ${MAXLEN} ))
      TPWD="${REPLACEMENT}${TPWD:${PWDOFFSET}:${MAXLEN}}"
    fi
    
    echo ${TPWD}
    

    Next you have to replace your PS1:

    export PS1="[\$(truncate.sh) ] "
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my current PS1 prompt definition from by .bashrc: PS1='\[\033[01;33m\]★ \[\033[01;30m\]\w \[\033[32m\]\$ \[\033[m\]'
I added this line to my .bashrc (Ubuntu 9.10): export PYTHONPATH=/opt/google_appengine/ And then I
I am having trouble getting PYTHONPATH set. I have added this line to ~/.bashrc
I've got this little nifty thing in my .bashrc : has_gitbranch() { if [
I add this line to /home/{user}/.bashrc file: export JAVA_HOME=/usr/lib/jvm/java-6-openjdk but when I want to
I have this in .bashrc; PS1='$' However, I see this still in terminal: mas-macbook:some/path
My .bashrc looks something like this... export PERL5LIB=/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0/${PLAT}-thread-multi export PERL5LIB=${PERL5LIB}:/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0 function dev { export
For example, when I say . .bashrc on my Linux command prompt, is there
Whenever I start a shell in vim using :sh, it doesn't source my ~/.bashrc
Hi I have added this alias to my .bashrc: alias awrco='svn co https://x.x.com/x/x/x/projects/' I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.