0

How can I ensure that PS1 variable in AIX bash is inherited between cross calling of scripts and programs ?

Suppose a program gives to the user a shell instance such as the shell command of vi. This can be used in two ways, in one of them it's launched by a script (see the 2nd case below):

  1. ksh prompt -> program -> "user asks for shell" -> ksh
  2. script -> program -> "user asks for shell" -> ksh

That works well with ksh. But when the bash is used (in AIX), we noticed in the 2nd case that the PS1 variable isn't inherited, so it has the default value.

You can test it just using vi, create a script, like: runvi.sh:

# blablabla
# vi $1

When we run the script and ask vi for the shell, the prompt is: sh-4.3$

Of course, when you run vi directly, and it asks for the shell, the prompt is your previously defined PS1.

The unique difference between the test above and the real program is that in the program the bash shows the PS1 with the value bash-4.3$, so the issue with PS1 inheritance seems the same. This C program can shows it:

#include <stdlib.h>
main() { system("$SHELL"); }

BASH VERSION:

bash-4.3$ bash -version
GNU bash, version 4.3.30(1)-release (powerpc-ibm-aix5.1.0.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

AIX VERSION

The same behavior between AIX 5.3 until 7.

OBS: In Ubuntu this doesn't happen.

  • 1
    You'll probably want system("$SHELL -li") to have it source your login config files. Many programs hardcode /bin/sh as a "system shell": in that case, you have no control. – glenn jackman Jul 7 '15 at 22:15
  • 1
    It sounds like you have a ~/.bashrc or similar that is resetting PS1. – Jeff Schaller Jul 8 '15 at 0:20
  • /etc/environment perhaps? Odd place for it, but it's unique to AIX. – Jeff Schaller Jul 8 '15 at 1:37
  • No @JeffSchaller. I tried a lot of things exploring .bashrc and ensured that they are the same as in Ubuntu. I also left just /etc/profile and removed all .bash* and .pro*. The same issue, but just in AIX – Luciano Jul 8 '15 at 12:09
  • @JeffSchaller, /etc/environment didn't produce effects (I didn't remove /etc/profile) – Luciano Jul 8 '15 at 12:14
0

An workaround:

// C code snippet
#define CMDPATTERN_PS1_PRESET "export PS1='%s';$SHELL"
#define CMDPATTERN_PS1_NOSET "$SHELL"

char* cmdpattern = (userPS1!=NULL && *userPS1 != NULL) ?CMDPATTERN_PS1_PRESET :CMDPATTERN_PS1_NOSET;
sprintf(shellcmd, cmdpattern, userPS1);
system(shellcmd);

So, if the user wants a custom PS1, in case it isn't inherited, it should be previsouly configured in the software. And can keep working in all shells and OSes.

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.