URL encoding a string in bash script
Solution 1
You want $MESSAGE
to be in double-quotes, so the shell won't split it into separate words, then pass it to PHP as an argument:
ENCODEDMESSAGE="$(php -r 'echo rawurlencode($argv[1]);' -- "$MESSAGE")"
Solution 2
On CentOS, no extra package needed:
python -c "import urllib;print urllib.quote(raw_input())" <<< "$message"
Solution 3
Extending Rockallite's very helpful answer for Python 3 and multiline input from a file (this time on Ubuntu, but that shouldn't matter):
cat any.txt | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))"
This will result in all lines from the file concatenated into a single URL, the newlines being replaced by %0A
.
Greg Alexander
I am a web developer by trade and currently own my own business VisionAmp Inc. I love my family, God and coding! http://visionamp.com
Updated on July 05, 2022Comments
-
Greg Alexander almost 2 years
I am writing a bash script in where I am trying to submit a post variable, however wget is treating it as multiple URLS I believe because it is not URLENCODED... here is my basic thought
MESSAGE='I am trying to post this information' wget -O test.txt http://xxxxxxxxx.com/alert.php --post-data 'key=xxxx&message='$MESSAGE''
I am getting errors and the alert.php is not getting the post variable plus it pretty mush is saying
can't resolve I can't resolve am can't resolve trying .. and so on.
My example above is a simple kinda sudo example but I believe if I can url encode it, it would pass, I even tried php like:
MESSAGE='I am trying to post this information' MESSAGE=$(php -r 'echo urlencode("'$MESSAGE'");')
but php errors out.. any ideas? How can i pass the variable in $MESSAGE without php executing it?
-
Darkman about 3 yearsLook for my answer here.
-
-
Leo Gallucci almost 7 yearsThis solution is python 2 only
-
Jason R. Coombs over 6 yearsPython 3 version looks like
python3 -c "import urllib.parse; print(urllib.parse.quote(input())"
. -
Anton K over 6 years@JasonR.Coombs you forgot one closing brace :)
-
Jason R. Coombs over 6 yearsA correct Python 3 version looks like
python3 -c "import urllib.parse; print(urllib.parse.quote(input()))"
(tested this time). -
Mike about 5 yearsPython version agnostic:
python -c $'try: import urllib.request as urllib\nexcept: import urllib\nimport sys\nsys.stdout.write(urllib.quote(input()))' <<< "$message"
and adds no\n
-
Sudip Bhattarai over 4 yearsNo this doesn't work. Replace
sys.stdin.read()
withinput()
instead. Using theread()
adds%0A
which is file termination character. -
Murphy over 4 yearsUsing
input()
would only return the first line, but my intention was to get all lines from the file, encoded in one URL. You could easily convert%0A
to newline again if necessary:[...] | sed "s/%0A/\n/g"
. -
iainH over 4 yearsWorks for me as posted by @Murphy (28/01/2020)