Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Consider this script using awk instead of sed for your case:</p> <pre><code>pwd_length=14 pwd_symbol="..." newPWD="${PWD/#$HOME/~}" if [ $(echo -n $newPWD | wc -c | tr -d " ") -gt $pwd_length ] then newPWD=$(echo -n $newPWD | awk -F '/' '{ print $1 "/" $2 "/.../" $(NF-1) "/" $(NF)}') fi PS1='${newPWD}$ ' </code></pre> <p>For your example of directory <code>~/workspace/projects/project1/folder1/test</code> it makes PS1 as: <code>~/workspace/.../folder1/test</code></p> <h1>UPDATE</h1> <p>Above solution will set your prompt but as you noted in your comment that it will NOT change PS1 dynamically when you change directory. So here is the solution that will dynamically set PS1 when you change directories around.</p> <p>Put these 2 lines in your .bashrc file:</p> <pre><code>export MYPS='$(echo -n "${PWD/#$HOME/~}" | awk -F "/" '"'"'{ if (length($0) &gt; 14) { if (NF&gt;4) print $1 "/" $2 "/.../" $(NF-1) "/" $NF; else if (NF&gt;3) print $1 "/" $2 "/.../" $NF; else print $1 "/.../" $NF; } else print $0;}'"'"')' PS1='$(eval "echo ${MYPS}")$ ' </code></pre> <p><code>if (NF &gt; 4 &amp;&amp; length($0) &gt; 14)</code> condition in awk will only apply special handling when your current directory is more than 3 directories deep AND if length of <code>$PWD</code> is more than 14 characters otherwise and it will keep PS1 as <code>$PWD</code>. </p> <p>eg: if current directory is <code>~/workspace/projects/project1$</code> then PS1 will be <code>~/workspace/projects/project1$</code></p> <p>Effect of above in .bashrc will be as follows on your PS1:</p> <pre><code>~$ cd ~/workspace/projects/project1/folder1/test ~/workspace/.../folder1/test$ cd .. ~/workspace/.../project1/folder1$ cd .. ~/workspace/.../project1$ cd .. ~/.../projects$ cd .. ~/workspace$ cd .. ~$ </code></pre> <p>Notice how prompt is changing when I change directories. Let me know if this is not what you wanted.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload