user6024347
user6024347

Reputation:

why cant i use & in python command line

I am trying to receive using python command line arguments but i have some problems in receiving & along with arguments

script

import sys
START_D = sys.argv[1]
print START_D

This works fine

python test.py sdsss
sdsss

But if i use & it is giving some random stuffs

python test.py sds&ss
[1] 8682
sds
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
u_seq  ESTAB      0      0      @0001b 25095                 * 25096                
u_seq  ESTAB      0      0      @0001a 25086                 * 25087                
u_seq  ESTAB      0      0      @00020 74036                
.
.
.
.
.
tcp    ESTAB      0      0      2405:205:830d:2a38:492a:5a32:cffe:eca1:34196                  2404:6800:4009:807::2003:https                
[1]+  Done                    python test.py sds

What was the issuse how can i fix it and i want to use & in parameters so how can i use it along when i send parameters?

Upvotes: 0

Views: 699

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121774

& is a metacharacter in your shell, and tells your shell to put the command in the background. This is why you see the PID of the backgrounded process echoed on your terminal:

[1] 8682

Escape it by using a backslash or quoting if you don't want the shell to interpret it:

$ python test.py sds\&ss
$ python test.py "sds&ss"

Upvotes: 6

Gábor Erdős
Gábor Erdős

Reputation: 3689

The sign & has a meaning in terminal. It means execute the first command in the background AND the second one in the front. Unfortunately ss is a valid command as well. You execute python test.py sds in the background and execute ss. ss is a network monitor protocol, that is why you see in those outputs.

Use your command like this: python test.py 'sds&ss'

Upvotes: 4

Related Questions