PS1 challenge

Ok then i Have a challenge for you :

Give me PS1 so that it always display the least 2 levels of directory
(except if i am above of course)

I want it this way :

so if i go to

/
/home/
/home/user
/home/user/whatever
/home/user/whatever1/whatever2 

my PS1 should respectively show the following :

ctsgnb@challenge:/ #
ctsgnb@challenge:/home #
ctsgnb@challenge:/home/user #
ctsgnb@challenge:/.../user/whatever1 #
ctsgnb@challenge:/.../whatever1/whatever2 #

i care about the /... if i go lower than 2 level !

This would be easier: While $PWDx has 3 slashes, take #?*/ off and put ...

ctsgnb@challenge:/ #
ctsgnb@challenge:/home #
ctsgnb@challenge:/home/user #
ctsgnb@challenge:.../user/whatever1 #
ctsgnb@challenge:.../whatever1/whatever2 #

something like

$(
pwdx="$PWD"
while [ "${pwdx#/*/*/" != "$pwdx" ]
do
 pwdx="${pwdx#/*}"
done
if [ "$pwdx" != "$PWD" ]
then
 pwdx=..."$pwdx"
fi
echo "$pwdx"
)

of course, quoting has to carry this all into PS1 for run time evaluation.

Easier ? where would be the fun ?
Come on guys i know you are far skilled enough to give a working solution ! :slight_smile:

Just add the slash, becasue, I found it was not easier, it just saves you a byte of precious line space, you ingrate! :smiley:

I long for a Scru1Liner solution proposal ... and see if he can do better :smiley:

I should use semicolons?
You think the world pays for byte count or maintainability?
This is no fork no exec solution!

I think that the fact of doing a

cd any_dir

should not use 100% of CPU or Memory :smiley:

To tell you the truth, i don't have the solution, i don't even know whether it is possible or not.:wink:

Sure, I did it, give it a try.

You don't want it to destroy your ability to type the next command.

# PS1=$(pwdx="$PWD" ; while [ "${pwdx#/*/*/" != "$pwdx" ] ; do pwdx="${pwdx#/*}" ; done ;if [ "$pwdx" != "$PWD" ] ; then pwdx=..."$pwdx" ; fi ; echo "$pwdx")
ksh: "${pwdx#/*/*/": bad substitution

---------- Post updated at 11:50 PM ---------- Previous update was at 11:49 PM ----------

... and my PS1 got ******* ... Scott , like this it's ok : it is as secret as a password :wink:

OK, let's give it a try :wink: in bash:

PS1="\u@\h:"'$([ "$PWD" != "${PWD%/*/*/*}" ] && echo "/...${PWD##${PWD%/*/*}}" || echo "$PWD")\$ '

---------- Post updated at 00:41 ---------- Previous update was at 00:28 ----------

ksh:

PS1="$(whoami)@$(hostname):"'$([ "$PWD" != "${PWD%/*/*/*}" ] && echo "/...${PWD##${PWD%/*/*}}" || echo "$PWD")\$ '

@Srcut1Linizer

i validate your solution, that one worked :

PS1=$LOGNAME@$(hostname):'$([ "$PWD" != "${PWD%/*/*/*}" ] && echo "/...${PWD##${PWD%/*/*}}" || echo "$PWD")\$ '

...maybe i should have asked for /dir/.../dir instead of /.../dir/dir :

PS1=$LOGNAME@$(hostname):'$([ "$PWD" != "${PWD%/*/*/*}" ] && echo "${PWD%%${PWD#/*/}}...${PWD##${PWD%/*}}" || echo "$PWD")\$ '

:smiley:

If in ksh you could always add this in your .kshrc:

CD(){
if [ $# -ne 0 ]
  then
    cd $1
  else
    cd $HOME
  fi
PS1="`uname -n`:..`echo $PWD | awk -F/ '{print $(NF-1)"/"$NF}'` > "
}
alias cd=CD

It's nicer if generating PS1 does not fork or exec!