简体   繁体   中英

create a new prompt in linux shell using c

I want to create a new prompt with a desired name in linux. And it should work like below.(if my executable name is out)

 original_shell$./out
 my_new_shell>give some input here
 ...
 ...
 some output
 ...
 my_new_shell>done
 original_shell$

How to do this using C, any help is appreciated!

You can do a setenv("PS1", "Your_new_prompt", 1);

I didn't test it but I think it's ok.

Hope it help.

You can use system() to run a new shell with PS1 set:

#include <stdlib.h>

int main() {
    system("PS1='my_new_shell>' bash");
    return 0;
}

Or you can use setenv() and then exec the shell:

#include <stdlib.h>
#include <unistd.h>

int main() {
    setenv("PS1", "my_new_shell>", 1);
    execlp("bash", "bash", (char*)NULL);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM