There is one new line to be expected. The echo command prints all its arguments on a single line, separated by spaces, which is the output you see.
You need to execute the result:
echo "$(ls %s)"
to keep newline characters in ls output. See Capturing multiline output to a Bash variable .
Using:
snprintf(command, sizeof(command), "echo \"$(ls %s)\" > something.txt", path);`
Of course, echo redundant; it would be better to just run:
ls %s
and therefore
snprintf(command, sizeof(command), "ls %s > something.txt", path);
If you save the echo, you should worry that the format string contains more than 20 additional characters, so 120 should be more than 130, if not 140.
You should also use scanf("%99s", path) (without ampersand; add a length limit) and ideally verify that it worked ( if (scanf(...) == 1) { ... OK ... } ).
source share