snipt

Ctrl+h for KB shortcuts

Bash

Script for use when setting PS1 prompt variable - shortens long `pwd`

#!/bin/bash

LENGTH="50"
HALF="$(($LENGTH/2))"

if [ ${#PWD} -gt $(($LENGTH)) ]; then
echo "${PWD:0:$(($HALF-3))}...${PWD:$((${#PWD}-$HALF)):$HALF}" | \
sed s/\\/home\\/$USER/~/
else
echo "$PWD" | sed s/\\/home\\/$USER/~/
fi
https://snipt.net/embed/4e000922622827b2d6974510abf4996c/
/raw/4e000922622827b2d6974510abf4996c/
4e000922622827b2d6974510abf4996c
bash
Bash
12
2019-05-26T19:58:55
True
False
False
/api/public/snipt/3969/
script-for-use-when-setting-ps1-prompt-variable-shortens-long-pwd
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="ch">#!/bin/bash</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nv">LENGTH</span><span class="o">=</span><span class="s2">&quot;50&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">HALF</span><span class="o">=</span><span class="s2">&quot;</span><span class="k">$((</span><span class="nv">$LENGTH</span><span class="o">/</span><span class="m">2</span><span class="k">))</span><span class="s2">&quot;</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="k">if</span> <span class="o">[</span> <span class="si">${#</span><span class="nv">PWD</span><span class="si">}</span> -gt <span class="k">$((</span><span class="nv">$LENGTH</span><span class="k">))</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span> </span><span id="L-7"><a name="L-7"></a><span class="nb">echo</span> <span class="s2">&quot;</span><span class="si">${</span><span class="nv">PWD</span><span class="p">:</span><span class="nv">0</span><span class="p">:</span><span class="k">$((</span><span class="nv">$HALF</span><span class="o">-</span><span class="m">3</span><span class="k">))</span><span class="si">}</span><span class="s2">...</span><span class="si">${</span><span class="nv">PWD</span><span class="p">:</span><span class="k">$((</span><span class="si">${#</span><span class="nv">PWD</span><span class="si">}</span><span class="o">-</span><span class="nv">$HALF</span><span class="k">))</span><span class="p">:</span><span class="nv">$HALF</span><span class="si">}</span><span class="s2">&quot;</span> <span class="p">|</span> <span class="se">\</span> </span><span id="L-8"><a name="L-8"></a>sed s/<span class="se">\\</span>/home<span class="se">\\</span>/<span class="nv">$USER</span>/~/ </span><span id="L-9"><a name="L-9"></a><span class="k">else</span> </span><span id="L-10"><a name="L-10"></a><span class="nb">echo</span> <span class="s2">&quot;</span><span class="nv">$PWD</span><span class="s2">&quot;</span> <span class="p">|</span> sed s/<span class="se">\\</span>/home<span class="se">\\</span>/<span class="nv">$USER</span>/~/ </span><span id="L-11"><a name="L-11"></a><span class="k">fi</span> </span></pre></div> </td></tr></table>
bash, prompt