0

I meet a problem about system function. If I run

echo -e '\x2f'

in shell,the output is / but when I put the command in C program like:

int main(int argc, char* argv[], char** envp)
{
    printf("The command is :%s\n",argv[1]);
    system( argv[1] );
    return 0;
}

output is:

The command is :echo -e '\x2f'
-e \x2f

Why does the system function output '-e \x2f' instead of '/'

BTW, I use Python to input the argv:

# I used \\ because python will transfer \x2f to / automatially
command="echo -e '\\x2f'"
p=process(executable='/home/cmd2',argv=   ['/home/cmd2',command])
print (p.readall())
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
ETenal
  • 3
  • 4

1 Answers1

1

Firstly, echo command can output differently between sh and bash.
Ref: https://unix.stackexchange.com/questions/88307/escape-sequences-with-echo-e-in-different-shells

bash -c "echo -e '\x2f'"
# Output : /
sh -c "echo -e '\x2f'"
# Output : -e /

In order to have Python spit out the same, something like below should work.
(For your reference, included the same implementation with subprocess)

import os
import subprocess

command = "echo -e '\\x2f'"

os.system( command )
# Output : -e /
subprocess.call( command , shell=True )
# Output : -e /

bashcmd = "bash -c \"echo -e '\x2f'\""

os.system( bashcmd )
# Output : /
subprocess.call( bashcmd , shell=True )
# Output : /

I am uncertain of how you got -e \x2f as your output though.

Community
  • 1
  • 1
Ryota
  • 690
  • 1
  • 8
  • 20
  • `bash -c \"echo -e '\x2f'\"` can work but my situation is that PATH variable has been emptied,so I use `echo -e '\x2f'`to construct `/`,but `/bin/bash` also have `/`,Maybe I should try another way – ETenal Nov 18 '16 at 04:23
  • Are you trying to set PATH variable ground up? It may be worth checking [this](http://stackoverflow.com/a/1681244/7153181) out. In any case, `echo` is probably not the way you want to take... – Ryota Nov 18 '16 at 23:57