I am trying to set a prompt for bash shell which is working however this particular system also uses Korn quite a bit.

echo ~/.profile -> export PS1="\[\033[38;5;10m\][\u@\h \W]\$ \[\]"

In Bash:

[root@mysystem ~]$ (in green)

In Korn:

[033[38;5;10m][u@h W]$ [] (default color)

I need to set it correctly for both shells.


From comments it sounds like you'd just like to check whether you're running bash, and in that case set the PS1 value for bash, and otherwise set it for ksh.

This is easy:

if [ -n "$BASH_VERSION" ]; then
    # set PS1 for bash
    # set PS1 for ksh (or non-bash in general)

[ -n "$BASH_VERSION" ] will be true if $BASH_VERSION is non-empty, which it will be if the current shell is bash.


Kusalananda's answer is good, but just in case someone decides to export BASH_VERSION before invoking ksh (thus fooling the test), you could instead test the value of $0:

case "$0" in
  (*ksh)  PS1="ksh version"     ;;
  (*bash) PS1="bash version"    ;;
  (*)     PS1="default version" ;;

This test is not infallible; if you ln -s /bin/bash /tmp/bashksh, then invoke /tmp/bashksh, the above code will falsely indicate that you are running ksh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.