Linux Mastering Development

Understanding command line arguments in custom shell and the effect of using quote

I am creating a custom shell using C language and I am successful with parsing, fork and exec, pipes, redirection etc. I noticed one particular type of command which seems to be throwing my shell off.

In bash shell , the following command works.

bash> echo "abc" >> tempFile
bash> sed  s/a/b/g  tempFile
bash> sed  's/a/b/g'  tempFile

In my custom shell,

mysh> sed  s/a/b/g  tempFile
mysh> sed  's/a/b/g'  tempFile

The first sed command works, the second fails as

sed: 1: "'s/a/b/g'": invalid command code '

This is how I fork and execute the command in my shell.

execvp(qualifiedPath, arguments)

In both the above command the qualifiedPath is "/usr/bin/sed" and the arguments are NULL terminated array of character pointer like
[0] = "sed",
[1] = "s/a/b/g",
[2] = "TEMP",
[3] = (char*)NULL
for the first command and
[0] = "sed",
[1] = "'s/a/b/g'",
[2] = "TEMP",
[3] = (char*)NULL for the second command respectively.

Does anyone know why the single quote causes exec to fail, and also the same behavior is observed for

mysh> sed  "s/a/b/g"  tempFile
sed: 1: "'s/a/b/g'": invalid command code '

Leave a Reply

Your email address will not be published. Required fields are marked *