Re: How do you set $PS1 on /bin/ksh
Date:Mon, 27 Jan 2020 11:42:13 + From:Ottavio Caruso Message-ID: | One more thing. Is there any trick I can | use to get $PWD expanded as "~" rather than "/home/oc"? See my earlier reply in this thread. kre
Re: How do you set $PS1 on /bin/ksh
Op 24/01/2020 om 18:56 schreef Ottavio Caruso: On Fri, 24 Jan 2020 at 16:34, Kamil Rytarowski wrote: On 24.01.2020 14:19, Ottavio Caruso wrote: Hi, [hoping my post doesn't arrive duplicated or triplicated] How do you set the prompt in ksh? The man page doesn't seem to help. OpenBSD ksh has a different manpage. Compare: https://man.openbsd.org/ksh.1#PS1 and https://netbsd.gw.com/cgi-bin/man-cgi?ksh For example: PS1="\u@\h:\w\$ " is not expanded. Thanks Personally, I use: export PS1='! $(whoami)@$(hostname) $PWD $ ' This works. Thanks. Thank you all for your help. One more thing. Is there any trick I can use to get $PWD expanded as "~" rather than "/home/oc"? This takes a lot of real estate. -- Ottavio Caruso
Re: How do you set $PS1 on /bin/ksh
In addition to the other recommendations, don't have the PS1 prompt run commands everytime the prompt is generated. For example, you don't need to run commands each prompt to figure out your username and hostname as likely they won't or cannot change in the same shell session. For example: PS1='`whoami`$ ' vs. PS1=`whoami`"$ "
Re: How do you set $PS1 on /bin/ksh
On Fri, Jan 24, 2020 at 07:05:29PM +, Ottavio Caruso wrote: > On Fri, 24 Jan 2020 at 18:57, Robert Elz wrote: > > > > There are a zillion different things called ksh, I'm not > > sure which version OpenBSD have as ksh > > Strangely enough, both OSes report exactly the same version: > > KSH_VERSION='@(#)PD KSH v5.2.14 99/07/13.2' That's because both are based on that version of pdksh. mksh is based on OpenBSD's ksh, not the other way around if I recall correctly. There was a flurry of activity on the OpenBSD side of the fence some time ago, when things these (backslash escapes for prompts) were added. (Actually, looking at it in CVS, that was 16 years ago now. Time flies.) -- Andreas (Kusalananda) Kähäri SciLifeLab, NBIS, ICM Uppsala University, Sweden .
Re: How do you set $PS1 on /bin/ksh
On Fri, 24 Jan 2020 at 16:34, Kamil Rytarowski wrote: > > On 24.01.2020 14:19, Ottavio Caruso wrote: > > Hi, > > > > [hoping my post doesn't arrive duplicated or triplicated] > > > > How do you set the prompt in ksh? The man page doesn't seem to help. > > OpenBSD ksh has a different manpage. Compare: > > https://man.openbsd.org/ksh.1#PS1 > > and > > https://netbsd.gw.com/cgi-bin/man-cgi?ksh > > > > For example: > > PS1="\u@\h:\w\$ " > > > > is not expanded. > > > > Thanks > > > > Personally, I use: > > export PS1='! $(whoami)@$(hostname) $PWD $ ' > This works. Thanks. -- Ottavio Caruso
Re: How do you set $PS1 on /bin/ksh
On Fri, 24 Jan 2020 at 18:57, Robert Elz wrote: > > There are a zillion different things called ksh, I'm not > sure which version OpenBSD have as ksh Strangely enough, both OSes report exactly the same version: KSH_VERSION='@(#)PD KSH v5.2.14 99/07/13.2' -- Ottavio Caruso
Re: How do you set $PS1 on /bin/ksh
Date:Fri, 24 Jan 2020 13:19:39 + From:Ottavio Caruso Message-ID: | How do you set the prompt in ksh? The same way one would set it in any other Bourne shell (more or less) compatible shell, PS1='whatever' | The man page doesn't seem to help. It looks reasonably accurate to me. | OpenBSD ksh has a different manpage. Compare: | https://man.openbsd.org/ksh.1#PS1 | and | https://netbsd.gw.com/cgi-bin/man-cgi?ksh There are a zillion different things called ksh, I'm not sure which version OpenBSD have as ksh (I believe they use mksh as /bin/sh so that variety of ksh might be used for both). | For example: | PS1="\u@\h:\w\$ " backslash escapes in PS1 are an abomination (someone added them in some version of ksh to attempt to be more csh compatible, but why anyone would want that restricted nonsense when there are better ways is beyond me). | is not expanded. It is, I expect (I'm not a ksh user so I can't guarantee it) but there are no appropriate expansion requests in there, as the man page says: PS1PS1 is the primary prompt for interactive shells. Parameter, command and arithmetic substitutions are performed, and ! is replaced with the current command number (see fc command below). There are no parameter, command, or arithmetic substitutions in that PS1 string, and no ! characters (another gross hack that was in the original ksh, also for csh compat reasons I believe) either. You can achieve most of what that expansion achieves using command or variable expansions (\u is $(id -un) \h is $(hostname -s) \w is trickier, as it hides $HOME with ~, but: $( X=${PWD#${HOME}}; case "$X" in "${PWD}") echo "$X";; *) echo "~$X";; esac ) should do it. Last \$ turns into $C after if [ $( id -u ) = 0 ]; then C=#; else C=$; fi (obviously use anything you prefer instead of 'C'). Other than the PWD expression, all of those can be expanded at definition time (unless you anticipate the hostname randomly changing) so defined in "" but the PWD evaluation needs to be at execution time so defined in '', so overall you'd end up with something like if [ $( id -u ) = 0 ]; then C=#; else C=$; fi PS1="$(id -un)@$(hostname -s):"'$( X=${PWD#${HOME}}; case "$X" in ("${PWD}") echo "$X";; (*) echo "~$X";; esac )'"$C " That actually works in ksh, as well as in /bin/sh bash mksh ksh93 (even yash and dash)... (Don't try it without the, what should be optional, leading '(' around the case patterns, while no other shell objects, our /bin/ksh does ... it is ancient and full of odd bugs). I'd suggest using /bin/sh instead (and if you do that, and need any command expansions actually performed at run time in PS1 (or PSanything) then "set -o promptcmds" first). For this kind of thing though, an alternative I prefer is to have what you want to be printed as the directory part in a variable, and then update that variable whenever you have successfully completed a cd command, something like cd() { command cd "$@" && CDIR=$( ) } and then just use ${CDIR} in the prompt, though I prefer to have the cd function embed it in my window's title bar instead when I am using windows in X (most of the time) so the prompt string stays short. kre ps: the reason this way is better than \ escapes is that it allows you to put anything you want in PS1 not just the half dozen things the csh authors decided you might want to stick in there. /bin/sh has a few special variables that make building dynamic prompts easier, like PSc is preset to be the # or $ character, no need for doing it yourself.
Re: How do you set $PS1 on /bin/ksh
On 24.01.2020 14:19, Ottavio Caruso wrote: > Hi, > > [hoping my post doesn't arrive duplicated or triplicated] > > How do you set the prompt in ksh? The man page doesn't seem to help. > OpenBSD ksh has a different manpage. Compare: > https://man.openbsd.org/ksh.1#PS1 > and > https://netbsd.gw.com/cgi-bin/man-cgi?ksh > > For example: > PS1="\u@\h:\w\$ " > > is not expanded. > > Thanks > Personally, I use: export PS1='! $(whoami)@$(hostname) $PWD $ ' signature.asc Description: OpenPGP digital signature
Re: How do you set $PS1 on /bin/ksh
Ottavio Caruso wrote in : |Hi, | |[hoping my post doesn't arrive duplicated or triplicated] | |How do you set the prompt in ksh? The man page doesn't seem to help. |OpenBSD ksh has a different manpage. Compare: |https://man.openbsd.org/ksh.1#PS1 |and |https://netbsd.gw.com/cgi-bin/man-cgi?ksh | |For example: |PS1="\u@\h:\w\$ " | |is not expanded. These things are totally non-portable. I setup a shell environment with some basics via ~/.profile (that is made to be found by all shells), and then interactive ~/.shrc (symlinked so to be found the way the shell(s) want it). And that uses the basic environment, for example $OSTYPE, $HOSTNAME etc., to create a shell prompt via variables. Somewhat heavily stripped (and embedded in a .profile environment) eval "___isinc=\$___SHRC$$" [ -z "${___isinc}" ] && { eval "___SHRC${$}=YES" export ___SHRC${$} case ${-} in *i*|*m*) ... # Determine shell type; aux while there ps1s= ps1S= ps1e= ps1W= case "${0}" in *ksh*) unset BASH_VERSION ___MKSH ___SHTYPE=ksh if [ "${KSH_VERSION}" != "${KSH_VERSION%%MIRBSD*}" ]; then export ___MKSH=YES eval "ps1s=\$'\e[31m' ps1S=\$'\e[38;5;203m' ps1e=\$'\e[0m'" # There were some problems in between.. if [ "${KSH_VERSION}" != "${KSH_VERSION%%R4[0-6]*}" ]; then trap 'echo; echo INTERRUPT' INT ___do_exit() { trap ___on_exit EXIT unalias exit exit } trap -- EXIT set -o ignoreeof alias exit=___do_exit fi bind ^O=delete-word-forward else ps1s="" ps1S="" ps1e="" # XXX \e <> OpenBSD? I think newer OpenBSD has support for \[..\], but i may be mistaken. This code is very (, very) old. fi ;; *bash*) unset KSH_VERSION ___MKSH ___SHTYPE=bash ps1s="\[\e[31m\]" ps1S="\[\e[38;5;203m\]" ps1e="\[\e[0m\]" shopt login_shell >/dev/null 2>&1 && trap -- EXIT ;; *yash*) unset BASH_VERSION KSH_VERSION ___MKSH ___SHTYPE=yash ps1s="\[\e[31m\]" ps1S="\[\e[38;5;203m\]" ps1e="\[\e[0m\]" set -o emacs ;; *) unset KSH_VERSION ___MKSH BASH_VERSION ___SHTYPE= # /bin/sh may be some BSD ash(1) if [ "${OSTYPE}" = freebsd ] || [ "${OSTYPE}" = dragonfly ]; then ps1W='\W' fi ;; esac export ___SHTYPE ... # Prompts are very complicated to get case "${TERM}" in *dumb*) ps1s= ps1S= ps1e=;; *256color*) ps1s=$ps1S;; *) if command -v tput >/dev/null 2>&1 && ( [ "`tput colors`" -ge 256 ] ); then ps1s=$ps1S fi ;; esac [ "${UID}" -eq 0 ] && PS1='#' || PS1='$' if ( [ "${HISTSIZE##84}" = 42 ] ) > /dev/null 2>&1 ; then # bash(1)/*ksh(1)? if [ -n "${___SHTYPE}" ]; then PS1="${ps1s}#?\$?|${HOSTNAME%%.*}:\${PWD##*/}${PS1}${ps1e} " else PS1="${ps1s}#${HOSTNAME%%.*}:${ps1W}${PS1}${ps1e} " fi else PS1="${ps1s}#${HOSTNAME}${PS1}${ps1e} " fi PS2='> ' export PS1 PS2 ... |Thanks What a mess. |-- |Ottavio Caruso --End of --steffen | |Der Kragenbaer,The moon bear, |der holt sich munter he cheerfully and one by one |einen nach dem anderen runter wa.ks himself off |(By Robert Gernhardt)
Re: How do you set $PS1 on /bin/ksh
Here's mine, # .shrc file for sh(1). ll(){ ls -l ${1+"$@"}; } case "$-" in *i*) if /bin/test -z "${HOST}"; then HOST="$(hostname)" fi PS1="${USER}@${HOST%%.*} $PS1" set -o emacs ;; esac Den fre 24 jan. 2020 17:30Ottavio Caruso skrev: > Hi, > > [hoping my post doesn't arrive duplicated or triplicated] > > How do you set the prompt in ksh? The man page doesn't seem to help. > OpenBSD ksh has a different manpage. Compare: > https://man.openbsd.org/ksh.1#PS1 > and > https://netbsd.gw.com/cgi-bin/man-cgi?ksh > > For example: > PS1="\u@\h:\w\$ " > > is not expanded. > > Thanks > > -- > Ottavio Caruso >
How do you set $PS1 on /bin/ksh
Hi, [hoping my post doesn't arrive duplicated or triplicated] How do you set the prompt in ksh? The man page doesn't seem to help. OpenBSD ksh has a different manpage. Compare: https://man.openbsd.org/ksh.1#PS1 and https://netbsd.gw.com/cgi-bin/man-cgi?ksh For example: PS1="\u@\h:\w\$ " is not expanded. Thanks -- Ottavio Caruso