Charan
Charan

Reputation: 21

setting IFS through system() in unix

I am trying to understand how IFS works with this simple code I have written. But this does not work, could anyone reason why this doesn't work?

#include <stdio.h>
void main()
{
    system("export IFS='/'; /bin/date");
}

According to the code above, the command '/bin/date' should be split into 2 commands as in 'bin' and 'date', but this is not happening.

Upvotes: 0

Views: 76

Answers (1)

William Pursell
William Pursell

Reputation: 212674

IFS is used for word splitting after expansion. There is no expansion happening here, so IFS is not relevant. There's no point in doing this via 'system', since that is just obfuscating the issue. Consider the following from the shell:

$ k=/bin/date
$ /bin/date  # execute /bin/date
$ $k   # expand k, perform word-splitting (nothing happens), run /bin/date
$ IFS=/
$ /bin/date  # execute /bin/date
$ $k   # expand k, perform word-splitting to get 3 words: "", "bin", "date"
       # attempt (and fail) to invoke a command with the name "" and arguments
       # "bin", "date"

Upvotes: 0

Related Questions