You need to quote (or escape) the &
character:
import os
def addToClipBoard(text):
command = "echo '{}' | clip".format(text.strip())
os.system(command)
addToClipBoard('&')
In Bash, &
is a control operator. It’s a shell builtin, meaning, it’s literally a core part of the Bash tool set. (Copied from BashItOut - Ampersands & on the command line)
EDIT
As @CharlesDuffy mentioned in the comments, there's a better way of doing this
for security reasons.
Consider the string $(rm -rf ~/*)'$(rm -rf ~/*)'
-- trying to copy it to the clipboard is going to result in a deleted home directory even with the extra quotes, because the quotes inside the string itself cancel them out.
The solution is to use shlex.quote()
as a safer means of adding literal quotes.
import os
import shlex
def addToClipBoard(text):
command = "printf '%s\n' {} | clip".format(shlex.quote(text))
os.system(command)
addToClipBoard('&')
NOTE: This last section is based on the comments by @CharlesDuffy.