# system() command

**URL:** <https://community.unix.com/t/system-command/226345>\
**Category:** Programming\
**Created:** [January 2, 2009, 8:54pm UTC](https://community.unix.com/t/system-command/226345 "2009-01-02T20:54:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicos](https://community.unix.com/letter_avatar/nicos/32/5_5575768a8748004e209b776fc1b2916d.png) [@nicos](https://community.unix.com/u/nicos)\
**Post date:** [January 2, 2009, 8:54pm UTC](https://community.unix.com/t/system-command/226345/1 "2009-01-02T20:54:27Z")

</div>

hello everybody!  
i write this following command: system ("time (md5sum ./a.out)\>log 2\>&1");  
as a part of a c program.  
but it printf the following error:  
sh: Syntax error: word unexpected (expecting ")")  
can anybody tell me why?

thanx in advance

---

<div class="post-metadata">

**Author:** ![reborg](https://community.unix.com/user_avatar/community.unix.com/reborg/32/167_2.png) [@reborg](https://community.unix.com/u/reborg)\
**Post date:** [January 2, 2009, 9:49pm UTC](https://community.unix.com/t/system-command/226345/2 "2009-01-02T21:49:33Z")

</div>

It is because you used a command which includes ().

---

<div class="post-metadata">

**Author:** ![nicos](https://community.unix.com/letter_avatar/nicos/32/5_5575768a8748004e209b776fc1b2916d.png) [@nicos](https://community.unix.com/u/nicos)\
**Post date:** [January 3, 2009, 9:19am UTC](https://community.unix.com/t/system-command/226345/3 "2009-01-03T09:19:41Z")

</div>

> [@reborg](#):
>
> It is because you used a command which includes ().

ok!i understood that! but how can i solve this problem? (beceause the same command work properly if i post it to the terminal)

thanks a lot!

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [January 4, 2009, 12:35pm UTC](https://community.unix.com/t/system-command/226345/4 "2009-01-04T12:35:44Z")

</div>

You could put the command string into an executable script and then call that script with the C system() call. Then test the shell script independently of the C program. Note the space characters on the line.

system( "myscript" )

I believe that the C system() call execs to /usr/bin/sh (see "man 3s system").  
The command syntax you are using is not valid in /usr/bin/sh .  
Not clear what you intended to capture into "log", but in sh the "(" must be the first character on the command line in that context.

Valid in sh:  
(time echo "hello world")

Invalid in sh:  
time (echo "hello world")

BTW. If you have syntax which requires a different shell, place the shell name at the top of the called script in the usual manner. e.g.:  
#!/usr/bin/myshell

---

<div class="post-metadata">

**Author:** ![mosaic](https://community.unix.com/letter_avatar/mosaic/32/5_5575768a8748004e209b776fc1b2916d.png) [@mosaic](https://community.unix.com/u/mosaic)\
**Post date:** [January 5, 2009, 12:28am UTC](https://community.unix.com/t/system-command/226345/5 "2009-01-05T00:28:19Z")

</div>

I think a backslash can sovle the problem. just like:

system ("time \\(md5sum ./a.out\\)\>log 2\>&1");

---

<div class="post-metadata">

**Author:** ![nicos](https://community.unix.com/letter_avatar/nicos/32/5_5575768a8748004e209b776fc1b2916d.png) [@nicos](https://community.unix.com/u/nicos)\
**Post date:** [January 5, 2009, 9:04am UTC](https://community.unix.com/t/system-command/226345/6 "2009-01-05T09:04:17Z")

</div>

thanx a lot everybody for the help!!!:b:🙂

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [January 5, 2009, 12:05pm UTC](https://community.unix.com/t/system-command/226345/7 "2009-01-05T12:05:46Z")

</div>

> [@methyl](#):
>
> I believe that the C system() call execs to /usr/bin/sh (see "man 3s system").

Often so, but this isn't 100% consistent from system to system since system() a stdio call, not a UNIX one. Probably best not to put complex shell commands of any sort inside a system() call.
