SdahlSean
SdahlSean

Reputation: 583

How to use a pure string as an argument for python program through bash terminal

I am trying to give an argument to my python program through the terminal. For this I am using the lines:

import sys
something = sys.argv[1]

I now try to put in a string like this through the bash terminal:

python my_script.py 2m+{N7HiwH3[>!"4y?t9*y@;/$Ar3wF9+k$[3hK/WA=aMzF°L0PaZTM]t*P|I_AKAqIb0O4@ cm=sl)WWYwEg10DDv%k/"c{LrS)oVd§4>8bs:;9u$ *W_SGk3CXe7hZMm$nXyhAuHDi-q+ug5+%ioou.,IhC]-_O§V]^,2q:VBVyTTD6'aNw9:oan(s2SzV

This returns a bash error because some of the characters in the string are bash special characters. How can I use the string exactly as it is?

Upvotes: 0

Views: 69

Answers (2)

hek2mgl
hek2mgl

Reputation: 158160

You can put the raw string into a file, for example like this, with cat and a here document.

cat <<'EOF' > file.txt
2m+{N7HiwH3[>!"4y?t9*y@;/$Ar3wF9+k$[3hK/WA=aMzF°L0PaZTM]t*P|I_AKAqIb0O4@ cm=sl)WWYwEg10DDv%k/"c{LrS)oVd§4>8bs:;9u$ *W_SGk3CXe7hZMm$nXyhAuHDi-q+ug5+%ioou.,IhC]-_O§V]^,2q:VBVyTTD6'aNw9:oan(s2SzV
EOF

and then run

python my_script.py "$(< file.txt)"

You can also use the text editor of your choice for the first step if you prefer that.


If this is a reoccurring task, which you have to perform from time to time, you can make your life easier with a little alias in your shell:

alias escape='read -r string ; printf "Copy this:\n%q\n" "${string}"'

It is using printf "%q" to escape your input string.

Run it like this:

escape
2m+{N7HiwH3[>!"4y?t9*y@;/$Ar3wF9+k$[3hK/WA=aMzF°L0PaZTM]t*P|I_AKAqIb0O4@ cm=sl)WWYwEg10DDv%k/"c{LrS)oVd§4>8bs:;9u$ *W_SGk3CXe7hZMm$nXyhAuHDi-q+ug5+%ioou.,IhC]-_O§V]^,2q:VBVyTTD6'aNw9:oan(s2SzV
Copy this:
2m+\{N7HiwH3\[\>\!\"4y\?t9\*y@\;/\$Ar3wF9+k\$\[3hK/WA=aMzF°L0PaZTM\]t\*P\|I_AKAqIb0O4@\ cm=sl\)WWYwEg10DDv%k/\"c\{LrS\)oVd§4\>8bs:\;9u\$\ \*W_SGk3CXe7hZMm\$nXyhAuHDi-q+ug5+%ioou.\,IhC\]-_O§V\]\^\,2q:VBVyTTD6\'aNw9:oan\(s2SzV

You can use the escaped string directly in your shell, without additional quotes, like this:

python my_script.py 2m+\{N7HiwH3\[\>\!\"4y\?t9\*y@\;/\$Ar3wF9+k\$\[3hK/WA=aMzF°L0PaZTM\]t\*P\|I_AKAqIb0O4@\ cm=sl\)WWYwEg10DDv%k/\"c\{LrS\)oVd§4\>8bs:\;9u\$\ \*W_SGk3CXe7hZMm\$nXyhAuHDi-q+ug5+%ioou.\,IhC\]-_O§V\]\^\,2q:VBVyTTD6\'aNw9:oan\(s2SzV

Upvotes: 1

ctt
ctt

Reputation: 1435

In order to make life easier, shells like bash do a little bit of extra work to help users pass the correct arguments to the programs they instruct it to execute. This extra work usually results in predictable argument arrays getting passed to programs.

Oftentimes, though, this extra help results in unexpected arguments getting passed to programs; and sometimes results in the execution of undesired additional commands. In this case, though, it ended up causing Bash to emit an error.

In order to turn off this extra work, Bash allows users to indicate where arguments should begin and end by surrounding them by quotation marks. Bash supports both single quotes (') and double quotes (") to delimit arguments. As a last resort, if a string may contain single and double quotes (or double quotes are required but aren't aggressive enough), Bash allows you to indicate that a special- or whitespace-character should be part of the adjacent argument by preceding it with a backslash (\\).

If this method of escaping arguments is too cumbersome, it may be worth simplifying your program's interface by having it consume this data from a file instead of a command line argument. Another option is to create a program that loads the arguments from a more controlled location (like a file) and directly execs the target program with the desired argument array.

Upvotes: 0

Related Questions