Find the answer to your Linux question:
Results 1 to 2 of 2
I'm trying to get nice escaped strings via bash. The script works fine so long as there aren't high ASCII (UTF-8) characters. Code: #!/bin/bash KKK="…hkjh'" echo ${KKK} printf "%b\n" "${KKK}" ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Posts
    5

    bash printf %q weirdness and UTF-8


    I'm trying to get nice escaped strings via bash. The script works fine so long as there aren't high ASCII (UTF-8) characters.

    Code:
    #!/bin/bash
    KKK="…hkjh'"
    echo ${KKK}
    printf "%b\n" "${KKK}"
    printf -v GGG "%q" "${KKK}"
    printf "%b\n" "${GGG}"
    and what I get is
    Code:
    …hkjh'
    …hkjh'
    $'…hkjh\''
    which is not what I was expecting (the leading $ and single quote wrappers).

    My shell is LANG=en_US.UTF-8 and it seems to handle the display of UTF-8 fine.

    The … is UTF code pt +2026 or 0xE280A6.

  2. #2
    Just Joined!
    Join Date
    Nov 2004
    Posts
    5
    So my hack is adding:

    Code:
    GGG=$(printf "%b\n" "${GGG}")
    if [[ "${GGG:0:1}" = "\$" ]]; then 
      LL=${#GGG}
      GGG="${GGG:2:($LL-3)}"
    fi
    echo $GGG
    which yields:
    Code:
    …hkjh\'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •