mkab 0 Light Poster

Hello everyone. I am to program a simple shell in C for my project. So far my shell can execute simple commands and now I want to implement enviroment variables. I looked up on how to use the getenv, setenv, putenv. So far so good i've tried to use the getenv to show the shell variables... with some succes . But I have a feeling that my reasoning is flawed. I have a char** cmdArgv which contains parsed input from the user input. I now check if cmdArgv starts with the command "echo" and then if any of the following inputs starts with a '$' sign. Here's my code:

int i = 0;
if(strcmp(cmdArgv[i], "echo") == 0){
                        char *variable;
                        for(i = 1; cmdArgv[i] != NULL; i++){
                            char* str = cmdArgv[i];
                            if( *(str + 0) == '$'){
                                //puts("i got a dollar");
                                variable = getenv(str + 1);
                                cmdArgv[i] = str;
                            }else{
                                variable = getenv(str);
                            }
                            if(!variable){
                                printf("%s ", cmdArgv[i]);
                            }else{
                                printf("%s ", variable);
                            }
                        }
                        printf("\n");
                        exit(0);
                    }

I think that normal linux shell finds the '$' sign, it expands the variable before invoking the echo command. My shell sn't following this principle, it is expanding variables inside the echo command itself. Any idea as to how I can implement this? Thanks.