I want to run a small UNIX shell script inside my C ++ program, and I want to write the script shell exit code. But the value returned by std :: system is not what I expect:
#include <iostream>
#include <cstdlib>
int main()
{
std::cout << std::system("echo Hello >/dev/null") << std::endl;
std::cout << std::system("which does_not_exisit 2>/dev/null") << std::endl;
std::cout << std::system("exit 0") << std::endl;
std::cout << std::system("exit 1") << std::endl;
std::cout << std::system("exit 2") << std::endl;
std::cout << std::system("exit 3") << std::endl;
std::cout << std::system("echo exit 4 | bash") << std::endl;
return 0;
}
In my Linux box, this produces:
0
256
0
256
512
768
1024
It seems that all exit codes greater than 0 are multiplied by 256. What is the reason for this behavior? Is this porting to UNIX like operating systems?
source
share