Skip to content

Instantly share code, notes, and snippets.

@sergeiwaigant
Last active January 9, 2026 21:10
Show Gist options
  • Select an option

  • Save sergeiwaigant/f0cf816e143114b6a7d81a901b5d3ce6 to your computer and use it in GitHub Desktop.

Select an option

Save sergeiwaigant/f0cf816e143114b6a7d81a901b5d3ce6 to your computer and use it in GitHub Desktop.

Revisions

  1. sergeiwaigant revised this gist Mar 26, 2025. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions url_encode_string_with_linux_tools.md
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,20 @@ Reference https://stackoverflow.com/a/34407620/13287790
    ```bash
    $ printf %s 'encode this'|jq -sRr @uri
    encode%20this

    $ jq -rn --arg x 'encode this' '$x|@uri'
    encode%20this
    -r (--raw-output) outputs the raw contents of strings instead of JSON string literals. -n (--null-input) doesn't read input from STDIN.

    -R (--raw-input) treats input lines as strings instead of parsing them as JSON, and -sR (--slurp --raw-input) reads the input into a single string. You can replace -sRr with -Rr if your input only contains a single line, or if you don't want to replace linefeeds with %0A:
    # -r (--raw-output) outputs the raw contents of strings instead of JSON string literals. -n (--null-input) doesn't read input from STDIN.
    # -R (--raw-input) treats input lines as strings instead of parsing them as JSON, and -sR (--slurp --raw-input) reads the input into a single string. You can replace -sRr with -Rr if your input only contains a single line, or if you don't want to replace linefeeds with %0A:

    $ printf %s\\n 'multiple lines' 'of text'|jq -Rr @uri
    multiple%20lines
    of%20text

    $ printf %s\\n 'multiple lines' 'of text'|jq -sRr @uri
    multiple%20lines%0Aof%20text%0A
    Or this percent-encodes all bytes:

    xxd -p|tr -d \\n|sed 's/../%&/g'
    ```
  2. sergeiwaigant renamed this gist Feb 4, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. sergeiwaigant revised this gist Feb 4, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Reference https://stackoverflow.com/a/34407620/13287790
    Reference https://stackoverflow.com/a/34407620/13287790

    ```bash
    $ printf %s 'encode this'|jq -sRr @uri
    encode%20this
  4. sergeiwaigant renamed this gist Feb 4, 2021. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions gistfile1.txt → readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    Another option is to use jq:

    # Reference https://stackoverflow.com/a/34407620/13287790

    ```bash
    $ printf %s 'encode this'|jq -sRr @uri
    encode%20this
    $ jq -rn --arg x 'encode this' '$x|@uri'
    @@ -17,4 +15,4 @@ $ printf %s\\n 'multiple lines' 'of text'|jq -sRr @uri
    multiple%20lines%0Aof%20text%0A
    Or this percent-encodes all bytes:

    xxd -p|tr -d \\n|sed 's/../%&/g'
    xxd -p|tr -d \\n|sed 's/../%&/g'
  5. sergeiwaigant created this gist Feb 4, 2021.
    20 changes: 20 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    Another option is to use jq:

    # Reference https://stackoverflow.com/a/34407620/13287790

    $ printf %s 'encode this'|jq -sRr @uri
    encode%20this
    $ jq -rn --arg x 'encode this' '$x|@uri'
    encode%20this
    -r (--raw-output) outputs the raw contents of strings instead of JSON string literals. -n (--null-input) doesn't read input from STDIN.

    -R (--raw-input) treats input lines as strings instead of parsing them as JSON, and -sR (--slurp --raw-input) reads the input into a single string. You can replace -sRr with -Rr if your input only contains a single line, or if you don't want to replace linefeeds with %0A:

    $ printf %s\\n 'multiple lines' 'of text'|jq -Rr @uri
    multiple%20lines
    of%20text
    $ printf %s\\n 'multiple lines' 'of text'|jq -sRr @uri
    multiple%20lines%0Aof%20text%0A
    Or this percent-encodes all bytes:

    xxd -p|tr -d \\n|sed 's/../%&/g'