Output of C++ function system(command) does not show color in Linux terminal

c++, linux, stdout, terminal

Solution

The answer for why there's no color lies here.

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.

`sh -c` ignores aliases. Perhaps somewhere you have an alias where `ls` means `ls --color=auto`.

So for example, if I do `sh -c 'ls'`, I will get no color.

Proof:

wow ♪[01:04 AM][vnbraun@chernobyl ~]$ which ls
alias ls='ls --color=auto'
        /bin/ls
wow ♪[01:08 AM][vnbraun@chernobyl ~]$ sh -c 'which ls'
/bin/ls

Therefore, you can try doing `system("ls --color=auto");`.

Problem

When I directly run a command in my Linux terminal, say "ls", the output is with color. However, when I run a C++ program which calls system("ls"), the output does not have color. Is there way to get the latter way to also display colored output? Thanks!

Original source