Find the answer to your Linux question:
Results 1 to 6 of 6
Hi All, I am looking to change the prompt in a simple custom shell implemented in c. I am using bash, so here is the relevant part of my main ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    2

    Change prompt in custom shell implementation programmed in C


    Hi All,
    I am looking to change the prompt in a simple custom shell implemented in c.

    I am using bash, so here is the relevant part of my main c program, with my code in red, italic and bold:

    #include <stdlib.h>

    int main(void) {

    system("PS1=""gfk$""");

    return 0;
    }

    My code compiles with no errors, but my system call to change the prompt to gfk$ does not work. I just get the default $ prompt.

    Any pointers, or code samples would be a great help!

    Sincerely,

    Gabor

  2. #2
    Just Joined!
    Join Date
    Sep 2010
    Posts
    3
    I am not entirely sure but I think programs are run by bash in there own environment so the PS1 variable in your environment wouldn't be changed by the program.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    First of all, if you're using Bash, it's not a custom shell implementation, I would think.

    mach1723 is entirely correct. A program cannot modify the environment variables of its parent. What you might be able to do is:
    Code:
    system("PS1=\"gfk$\" bash");
    This would cause your program to launch a new instance of Bash with the PS1 variable set to what you want. When you exit that instance of Bash, you will be back in the original instance, with its original prompt.

    Note that a better way to configure Bash is to use the ~/.bashrc file, where you can set whatever variables you want. Bash will read this file every time it starts, so you can configure every Bash prompt however you want from one file.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Sep 2010
    Posts
    2
    I completetly agree with you both, however this is part a programming assignment I got in school. All other bits I handled without any problem, but to modify the damn cursor is totally stumping me.

    So there is an actual implementation of a custom shell programmed in C. I have to modify the code so that when the custom shell launches it displays a prompt of my liking instead of the default $.

    I am modifying and running the program in Ubuntu, so my underlaying shell is bash. I am using terminal to make, compile and execute the custom shell program, which I need to modify.

    I hope this clears things up a bit.

    Thank you again,
    Sincerely,
    Gabor

  6. #5
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    I actually don't know what you exactly mean.
    You have an own implementation of bash or something like that which you execute on bash?
    As Cabhan said you only open one shell process, set PS1 to your custom value and then exit this shell. You can only set an environment variable for your current shell and when you export it (export PS1="abc") also for subshells and other subprocesses but not for the parent process or in general. To modify the prompt globally you can only use your ~/.bashrc resp. ~/.profile or modify the source code of your shell.
    But maybe that's what your teacher wants you to do:
    Code:
    $ export PS1="foo "
    foo ./mycustomshell
    foo PS1="bar "
    bar exit
    foo _

  7. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    As per the forum rules, homework questions are not allowed on this forum:
    http://www.linuxforums.org/forum/lin...ums-rules.html

    If you have a question on a concept, we're happy to help, but we do not solve homework problems.

    Closing thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •