Resolve the problem caused by Shell executing python file and passing parameter Spaces

  • 2021-01-18 06:30:56
  • OfStack

Using shell, call an python file and pass arguments to shell, for example:


p1='wang'
p2='shuang'
python py file  $p1 $p2

This can be done normally, and the py file accepts p1 and p2

However, a problem occurs when there are Spaces in ES12en1:


p1='wa ng'
p2='shuang'
python py file  $p1 $p2

The py file receives the first argument as wa and the second argument as ng, thus causing an error.

Solution: Put double quotes


p1='wa ng'
p2='shuang'
python py file  "$p1" "$p2"

In this way, the py file receives no error arguments, the first is wa ng, and the second is shuang


Related articles: