How do you get different $PS1 for /bin/sh and /bin/ksh?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How do you get different $PS1 for /bin/sh and /bin/ksh?

Ottavio Caruso
Hi,

I have this in ~/.kshrc :

PS1="\u@\h:\w\$ "

which works fine in ksh:

oc@OpenBSD:~$

However, if I open a sh subshell, I get:

\u@OpenBSD:\w$

which is not very nice. The only hack I've found is to append this to
~/.profile:


if [ -n "$KSH_VERSION" ]; then
     if [ -f "$HOME/.kshrc" ]; then
        . "$HOME/.kshrc"
     fi
fi

I wonder if there is a more elegant solution.


--
Ottavio Caruso

Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Vincenzo Nicosia
On Tue, Sep 15, 2020 at 02:08:16PM +0100, Ottavio Caruso wrote:

> Hi,
>
> I have this in ~/.kshrc :
>
> PS1="\u@\h:\w\$ "
>
> which works fine in ksh:
>
> oc@OpenBSD:~$
>
> However, if I open a sh subshell, I get:
>
> \u@OpenBSD:\w$
>
> which is not very nice. The only hack I've found is to append this to
> ~/.profile:
>
>
> if [ -n "$KSH_VERSION" ]; then
>     if [ -f "$HOME/.kshrc" ]; then
> . "$HOME/.kshrc"
>     fi
> fi
>
> I wonder if there is a more elegant solution.

Hi,

the more elegant solution is to set ENV appropriately. ~/.profile is
normally read only at login, while sub-shells will source whatever
file is specified in ENV.

HTH

--

Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Ottavio Caruso
On 15/09/2020 14:44, Vincenzo Nicosia wrote:

> On Tue, Sep 15, 2020 at 02:08:16PM +0100, Ottavio Caruso wrote:
>> Hi,
>>
>> I have this in ~/.kshrc :
>>
>> PS1="\u@\h:\w\$ "
>>
>> which works fine in ksh:
>>
>> oc@OpenBSD:~$
>>
>> However, if I open a sh subshell, I get:
>>
>> \u@OpenBSD:\w$
>>
>> which is not very nice. The only hack I've found is to append this to
>> ~/.profile:
>>
>>
>> if [ -n "$KSH_VERSION" ]; then
>>      if [ -f "$HOME/.kshrc" ]; then
>> . "$HOME/.kshrc"
>>      fi
>> fi
>>
>> I wonder if there is a more elegant solution.
>
> Hi,
>
> the more elegant solution is to set ENV appropriately. ~/.profile is
> normally read only at login, while sub-shells will source whatever
> file is specified in ENV.

This is what I had and it generated the undesired prompt in sh.

You probably mean "interactive shells", not just sub shells. A sub shell
could be child of a non login shell as well.

With ENV=~/.khsrc, PS1 is propagated over to sub shells, whether it is
ksh or sh, whereas just sourcing ./.khsrc  will not export PS1. I don't
know why this is. It doesn't make sense to me. Maybe it's just because
OpenBSD sh is just ksh in disguise or there might be other reasons that
I obviously don't know.

--
Ottavio Caruso

Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

eurydice
On 20/09/15 05:49PM, Ottavio Caruso wrote:
> Maybe it's just because OpenBSD sh is just ksh in disguise or there
> might be other reasons that I obviously don't know.

Yep, you're right. They share the same inode.

ls -li /bin/{,k}sh

77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/ksh
77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/sh

sh(1) also attests to this.

--
https://amissing.link

Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Ottavio Caruso
On 17/09/2020 00:58, Ashlen wrote:

> On 20/09/15 05:49PM, Ottavio Caruso wrote:
>> Maybe it's just because OpenBSD sh is just ksh in disguise or there
>> might be other reasons that I obviously don't know.
>
> Yep, you're right. They share the same inode.
>
> ls -li /bin/{,k}sh
>
> 77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/ksh
> 77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/sh
>
> sh(1) also attests to this.

Thanks but I gave that for granted. My question was about not exporting
PS1 to subshells. In theory, it shouldn't be exported but it does get
exported if one uses ENV=.kshrc vs sourcing .kshrc.


--
Ottavio Caruso


Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Tom H
On Thu, Sep 17, 2020 at 9:33 AM Ottavio Caruso
<[hidden email]> wrote:

> On 17/09/2020 00:58, Ashlen wrote:
>> On 20/09/15 05:49PM, Ottavio Caruso wrote:
>>>
>>> Maybe it's just because OpenBSD sh is just ksh in disguise or there
>>> might be other reasons that I obviously don't know.
>>
>> Yep, you're right. They share the same inode.
>>
>> ls -li /bin/{,k}sh
>>
>> 77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/ksh
>> 77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/sh
>>
>> sh(1) also attests to this.
>
> Thanks but I gave that for granted. My question was about not
> exporting PS1 to subshells. In theory, it shouldn't be exported but
> it does get exported if one uses ENV=.kshrc vs sourcing .kshrc.

You've said that you're now sourcing "$HOME/.kshrc" if "SKSH_VERSION" exists.

You could add the sourcing of "$HOME/.shrc" if "$SH_VERSION" exists.

Or you could export ENV and use a case-esac of this kind:

case "$0" in
*ksh)
...
PS1='\u@\h:\w\$ '
;;
*sh)
...
PS1='${USER}@${HOST}:${PWD}\$ '
;;
esac

Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Ottavio Caruso
On 17/09/2020 10:40, Tom H wrote:

> On Thu, Sep 17, 2020 at 9:33 AM Ottavio Caruso
> <[hidden email]> wrote:
>> On 17/09/2020 00:58, Ashlen wrote:
>>> On 20/09/15 05:49PM, Ottavio Caruso wrote:
>>>>
>>>> Maybe it's just because OpenBSD sh is just ksh in disguise or there
>>>> might be other reasons that I obviously don't know.
>>>
>>> Yep, you're right. They share the same inode.
>>>
>>> ls -li /bin/{,k}sh
>>>
>>> 77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/ksh
>>> 77862 -r-xr-xr-x  3 root  bin  613656 Sep 15 12:10 /bin/sh
>>>
>>> sh(1) also attests to this.
>>
>> Thanks but I gave that for granted. My question was about not
>> exporting PS1 to subshells. In theory, it shouldn't be exported but
>> it does get exported if one uses ENV=.kshrc vs sourcing .kshrc.
>
> You've said that you're now sourcing "$HOME/.kshrc" if "SKSH_VERSION" exists.
>
> You could add the sourcing of "$HOME/.shrc" if "$SH_VERSION" exists.
>
> Or you could export ENV and use a case-esac of this kind:
>
> case "$0" in
> *ksh)
> ...
> PS1='\u@\h:\w\$ '
> ;;
> *sh)
> ...
> PS1='${USER}@${HOST}:${PWD}\$ '
> ;;
> esac
>
>

This solves the problem. Thanks.

--
Ottavio Caruso



Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Tom H
On Thu, Sep 17, 2020 at 3:16 PM Ottavio Caruso
<[hidden email]> wrote:

> On 17/09/2020 10:40, Tom H wrote:
>>
>> You've said that you're now sourcing "$HOME/.kshrc" if
>>"SKSH_VERSION" exists.
>>
>> You could add the sourcing of "$HOME/.shrc" if "$SH_VERSION" exists.
>>
>> Or you could export ENV and use a case-esac of this kind:
>>
>> case "$0" in
>> *ksh)
>> ...
>> PS1='\u@\h:\w\$ '
>> ;;
>> *sh)
>> ...
>> PS1='${USER}@${HOST}:${PWD}\$ '
>> ;;
>> esac
>
> This solves the problem. Thanks.

You're welcome.

But, out of curiosity, which option did you choose? TIA

Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Ottavio Caruso
On 18/09/2020 09:01, Tom H wrote:

> On Thu, Sep 17, 2020 at 3:16 PM Ottavio Caruso
> <[hidden email]> wrote:
>> On 17/09/2020 10:40, Tom H wrote:
>>>
>>> You've said that you're now sourcing "$HOME/.kshrc" if
>>> "SKSH_VERSION" exists.
>>>
>>> You could add the sourcing of "$HOME/.shrc" if "$SH_VERSION" exists.
>>>
>>> Or you could export ENV and use a case-esac of this kind:
>>>
>>> case "$0" in
>>> *ksh)
>>> ...
>>> PS1='\u@\h:\w\$ '
>>> ;;
>>> *sh)
>>> ...
>>> PS1='${USER}@${HOST}:${PWD}\$ '
>>> ;;
>>> esac
>>
>> This solves the problem. Thanks.
>
> You're welcome.
>
> But, out of curiosity, which option did you choose? TIA

Ah sorry,  I used the second option. I have this in .profile:

export ENV="$HOME/.kshrc"


and this in .kshrc:


case "$0" in
*ksh)
PS1='\u@\h:\w\$ '
;;
*sh)
PS1='${USER}@${HOST}:${PWD}\$ '
;;
esac


On my NetBSD VM, sh and ksh are two different executables. ENV points to
~/.shrc which then sources ./.kshrc if KSH_VERSION = true

I have though of replicating the same configuration over to OpenBSD but
I might be looking for trouble.

On a side note, there's no mention of startup files in sh(1) and I
wonder why.


--
Ottavio Caruso



Reply | Threaded
Open this post in threaded view
|

Re: How do you get different $PS1 for /bin/sh and /bin/ksh?

Ingo Schwarze
Hi Ottavio,

Ottavio Caruso wrote on Fri, Sep 18, 2020 at 09:22:11AM +0100:

> On a side note, there's no mention of startup files in sh(1)
> and I wonder why.

From sh(1), second paragraph:

  This manual page describes only the parts relevant to a POSIX
  compliant sh.  If portability is a concern, use only those
  features described in this page.

POSIX does not require that the shell handles any startup files,
neither any with "profile" in the name, nor any with "rc" in the name,
nor any with "login" in the name, see

  https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html

I does require ENV though, and consequently, sh(1) mentions that.

Yours,
  Ingo