Problems escaping a variable when using echo

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
2












I'm trying to escape the following code with the echo command but I keep getting the actual octet and not the emoji.



Also where could I find the octet values of the emojis? I seem to always find the UTF-8 values.



#!/usr/bin/env bash

UNICORN='360237246204n'
FIRE=''

# this does not work when I run the script
printf '360237246204n'
printf "Riding a $UNICORN:Q"
echo "Riding a $UNICORN:Q" #[Fails]: how to extract the actual emoji?


EDIT_1: Just updating the code after reading comments



#!/usr/bin/env bash
# Note: use hexdump -b to get one-bye octal display

UNICORN_UTF8=$'360237246204'


printf "U1F525n"|hexdump -b # [ASK]: How to translate the return value to a valid UTF8 ?

FIRE_UTF8=$'1256110665626512'

echo "Riding a $UNICORN_locale_encoding"
echo "$UNICORN_UTF8 + $FIRE_UTF8"


EDIT_2: Posting final code. It sort of works.



#!/usr/bin/env bash

# Author:
# Usage:
# Note: use hexdump -b to get one-bye octal display of the emoji (needed for when ≠ computers use ≠ commandLine tools)
# Ex: printf "U1F525n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo

UNICORN_UTF8=$'360237246204'
FIRE_UTF8=$'xF0x9Fx94xA5'
LEAVE_SPACE=^[a-zA-Z0-9_]*$

echo "Riding an $UNICORN_UTF8 $LEAVE_SPACE out of a $FIRE_UTF8 $LEAVE_SPACE house."






share|improve this question









New contributor




intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 3




    Welcome to this site! Any reason why you don't want to use printf? Also see this relevant question unix.stackexchange.com/questions/65803/…
    – Mr Shunz
    Sep 6 at 9:51






  • 2




    This is a follow-on question to unix.stackexchange.com/questions/466961 .
    – JdeBP
    Sep 6 at 9:53






  • 1




    octet and even n are printf syntax. what makes you expect echo knos ho to interpret them?
    – Philippos
    Sep 6 at 9:54











  • @Philippos thanks, I thought that printf and echo behaved the same. New to this bash and scripting. @MrShunz, main reason is because I would like to use it on an script, and my current scripts has echo "Ready to git-some and sync your local branches to the remote counterparts ?"
    – intercoder
    Sep 6 at 14:02










  • @JdeBP correct that is the follow up question to actually all my questions here. I'm trying to incorporate emojis into my script
    – intercoder
    Sep 6 at 14:16














up vote
2
down vote

favorite
2












I'm trying to escape the following code with the echo command but I keep getting the actual octet and not the emoji.



Also where could I find the octet values of the emojis? I seem to always find the UTF-8 values.



#!/usr/bin/env bash

UNICORN='360237246204n'
FIRE=''

# this does not work when I run the script
printf '360237246204n'
printf "Riding a $UNICORN:Q"
echo "Riding a $UNICORN:Q" #[Fails]: how to extract the actual emoji?


EDIT_1: Just updating the code after reading comments



#!/usr/bin/env bash
# Note: use hexdump -b to get one-bye octal display

UNICORN_UTF8=$'360237246204'


printf "U1F525n"|hexdump -b # [ASK]: How to translate the return value to a valid UTF8 ?

FIRE_UTF8=$'1256110665626512'

echo "Riding a $UNICORN_locale_encoding"
echo "$UNICORN_UTF8 + $FIRE_UTF8"


EDIT_2: Posting final code. It sort of works.



#!/usr/bin/env bash

# Author:
# Usage:
# Note: use hexdump -b to get one-bye octal display of the emoji (needed for when ≠ computers use ≠ commandLine tools)
# Ex: printf "U1F525n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo

UNICORN_UTF8=$'360237246204'
FIRE_UTF8=$'xF0x9Fx94xA5'
LEAVE_SPACE=^[a-zA-Z0-9_]*$

echo "Riding an $UNICORN_UTF8 $LEAVE_SPACE out of a $FIRE_UTF8 $LEAVE_SPACE house."






share|improve this question









New contributor




intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 3




    Welcome to this site! Any reason why you don't want to use printf? Also see this relevant question unix.stackexchange.com/questions/65803/…
    – Mr Shunz
    Sep 6 at 9:51






  • 2




    This is a follow-on question to unix.stackexchange.com/questions/466961 .
    – JdeBP
    Sep 6 at 9:53






  • 1




    octet and even n are printf syntax. what makes you expect echo knos ho to interpret them?
    – Philippos
    Sep 6 at 9:54











  • @Philippos thanks, I thought that printf and echo behaved the same. New to this bash and scripting. @MrShunz, main reason is because I would like to use it on an script, and my current scripts has echo "Ready to git-some and sync your local branches to the remote counterparts ?"
    – intercoder
    Sep 6 at 14:02










  • @JdeBP correct that is the follow up question to actually all my questions here. I'm trying to incorporate emojis into my script
    – intercoder
    Sep 6 at 14:16












up vote
2
down vote

favorite
2









up vote
2
down vote

favorite
2






2





I'm trying to escape the following code with the echo command but I keep getting the actual octet and not the emoji.



Also where could I find the octet values of the emojis? I seem to always find the UTF-8 values.



#!/usr/bin/env bash

UNICORN='360237246204n'
FIRE=''

# this does not work when I run the script
printf '360237246204n'
printf "Riding a $UNICORN:Q"
echo "Riding a $UNICORN:Q" #[Fails]: how to extract the actual emoji?


EDIT_1: Just updating the code after reading comments



#!/usr/bin/env bash
# Note: use hexdump -b to get one-bye octal display

UNICORN_UTF8=$'360237246204'


printf "U1F525n"|hexdump -b # [ASK]: How to translate the return value to a valid UTF8 ?

FIRE_UTF8=$'1256110665626512'

echo "Riding a $UNICORN_locale_encoding"
echo "$UNICORN_UTF8 + $FIRE_UTF8"


EDIT_2: Posting final code. It sort of works.



#!/usr/bin/env bash

# Author:
# Usage:
# Note: use hexdump -b to get one-bye octal display of the emoji (needed for when ≠ computers use ≠ commandLine tools)
# Ex: printf "U1F525n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo

UNICORN_UTF8=$'360237246204'
FIRE_UTF8=$'xF0x9Fx94xA5'
LEAVE_SPACE=^[a-zA-Z0-9_]*$

echo "Riding an $UNICORN_UTF8 $LEAVE_SPACE out of a $FIRE_UTF8 $LEAVE_SPACE house."






share|improve this question









New contributor




intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










I'm trying to escape the following code with the echo command but I keep getting the actual octet and not the emoji.



Also where could I find the octet values of the emojis? I seem to always find the UTF-8 values.



#!/usr/bin/env bash

UNICORN='360237246204n'
FIRE=''

# this does not work when I run the script
printf '360237246204n'
printf "Riding a $UNICORN:Q"
echo "Riding a $UNICORN:Q" #[Fails]: how to extract the actual emoji?


EDIT_1: Just updating the code after reading comments



#!/usr/bin/env bash
# Note: use hexdump -b to get one-bye octal display

UNICORN_UTF8=$'360237246204'


printf "U1F525n"|hexdump -b # [ASK]: How to translate the return value to a valid UTF8 ?

FIRE_UTF8=$'1256110665626512'

echo "Riding a $UNICORN_locale_encoding"
echo "$UNICORN_UTF8 + $FIRE_UTF8"


EDIT_2: Posting final code. It sort of works.



#!/usr/bin/env bash

# Author:
# Usage:
# Note: use hexdump -b to get one-bye octal display of the emoji (needed for when ≠ computers use ≠ commandLine tools)
# Ex: printf "U1F525n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo

UNICORN_UTF8=$'360237246204'
FIRE_UTF8=$'xF0x9Fx94xA5'
LEAVE_SPACE=^[a-zA-Z0-9_]*$

echo "Riding an $UNICORN_UTF8 $LEAVE_SPACE out of a $FIRE_UTF8 $LEAVE_SPACE house."








share|improve this question









New contributor




intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Rui F Ribeiro

36.1k1271115




36.1k1271115






New contributor




intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Sep 6 at 9:45









intercoder

1276




1276




New contributor




intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






intercoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 3




    Welcome to this site! Any reason why you don't want to use printf? Also see this relevant question unix.stackexchange.com/questions/65803/…
    – Mr Shunz
    Sep 6 at 9:51






  • 2




    This is a follow-on question to unix.stackexchange.com/questions/466961 .
    – JdeBP
    Sep 6 at 9:53






  • 1




    octet and even n are printf syntax. what makes you expect echo knos ho to interpret them?
    – Philippos
    Sep 6 at 9:54











  • @Philippos thanks, I thought that printf and echo behaved the same. New to this bash and scripting. @MrShunz, main reason is because I would like to use it on an script, and my current scripts has echo "Ready to git-some and sync your local branches to the remote counterparts ?"
    – intercoder
    Sep 6 at 14:02










  • @JdeBP correct that is the follow up question to actually all my questions here. I'm trying to incorporate emojis into my script
    – intercoder
    Sep 6 at 14:16












  • 3




    Welcome to this site! Any reason why you don't want to use printf? Also see this relevant question unix.stackexchange.com/questions/65803/…
    – Mr Shunz
    Sep 6 at 9:51






  • 2




    This is a follow-on question to unix.stackexchange.com/questions/466961 .
    – JdeBP
    Sep 6 at 9:53






  • 1




    octet and even n are printf syntax. what makes you expect echo knos ho to interpret them?
    – Philippos
    Sep 6 at 9:54











  • @Philippos thanks, I thought that printf and echo behaved the same. New to this bash and scripting. @MrShunz, main reason is because I would like to use it on an script, and my current scripts has echo "Ready to git-some and sync your local branches to the remote counterparts ?"
    – intercoder
    Sep 6 at 14:02










  • @JdeBP correct that is the follow up question to actually all my questions here. I'm trying to incorporate emojis into my script
    – intercoder
    Sep 6 at 14:16







3




3




Welcome to this site! Any reason why you don't want to use printf? Also see this relevant question unix.stackexchange.com/questions/65803/…
– Mr Shunz
Sep 6 at 9:51




Welcome to this site! Any reason why you don't want to use printf? Also see this relevant question unix.stackexchange.com/questions/65803/…
– Mr Shunz
Sep 6 at 9:51




2




2




This is a follow-on question to unix.stackexchange.com/questions/466961 .
– JdeBP
Sep 6 at 9:53




This is a follow-on question to unix.stackexchange.com/questions/466961 .
– JdeBP
Sep 6 at 9:53




1




1




octet and even n are printf syntax. what makes you expect echo knos ho to interpret them?
– Philippos
Sep 6 at 9:54





octet and even n are printf syntax. what makes you expect echo knos ho to interpret them?
– Philippos
Sep 6 at 9:54













@Philippos thanks, I thought that printf and echo behaved the same. New to this bash and scripting. @MrShunz, main reason is because I would like to use it on an script, and my current scripts has echo "Ready to git-some and sync your local branches to the remote counterparts ?"
– intercoder
Sep 6 at 14:02




@Philippos thanks, I thought that printf and echo behaved the same. New to this bash and scripting. @MrShunz, main reason is because I would like to use it on an script, and my current scripts has echo "Ready to git-some and sync your local branches to the remote counterparts ?"
– intercoder
Sep 6 at 14:02












@JdeBP correct that is the follow up question to actually all my questions here. I'm trying to incorporate emojis into my script
– intercoder
Sep 6 at 14:16




@JdeBP correct that is the follow up question to actually all my questions here. I'm trying to incorporate emojis into my script
– intercoder
Sep 6 at 14:16










2 Answers
2






active

oldest

votes

















up vote
10
down vote



accepted










echo's syntax is different from the standard C escapes as supported by printf/awk/$'...'...



In standard echo syntax, you need a leading 0 in front of the octal sequence (which can have from 1 to 3 digits)¹:



echo '360237246204'


Note that for bash's echo builtin to work with that, you need to enable the xpg_echo option²:



$ UNICORN_utf8_printf_format='360237246204'
$ UNICORN_utf8_echo='360237246204'
$ UNICORN_utf8=$'360237246204'
$ printf "$UNICORN_utf8_printf_formatn"
🦄
$ printf '%sn' "$UNICORN_utf8"
🦄
$ shopt -s xpg_echo
$ echo "$UNICORN_utf8_echo"
🦄


Above, only $UNICORN_utf8 contains a 🦄 character, encoded in UTF8. The other ones contain sequences of backslash and digits that are meant to be expanded by the respective tools.



The %b format of the printf utility also understands the same sequences as echo. %b was actually added so we can get rid of echo which is impossible to use portably and reliably.



$ printf '%bn' "$UNICORN_utf8_echo"
🦄


See also (in zsh and bash³):



UNICORN_locale_encoding=$'U1f984'


Which gets you a Unicorn encoded in the locale's encoding, which would make it work even if the locale's encoding was not UTF-8 and also had that character (probably only GB18030, where 🦄 is encoded as $'2256033066' and where $'360237246204' would be the encoding of 馃 (NCJK UNIFIED IDEOGRAPH-9983N<private use area-E6E9>)).



Some printf implementations (including GNU printf and the printf builtin of zsh, ksh93 and recent versions of bash (4.2 or above)) also support those UXXXXXXXX escape sequences in their format argument (or arguments to %b except with ksh93); the GNU one needs 8 digits.




¹ GNU coreutils echo and busybox echo support ooo with -e as an extension (not when POSIXLY_CORRECT is in the environment for GNU echo)



² other option would be to use the non-standard -e option, but then it wouldn't work when both the posix and xpg_echo options are enabled, like when bash is in UNIX compliance mode.



³ ksh93 and mksh also support that syntax, but encode in UTF-8 regardless of the locale's encoding; in current (2018) versions of FreeBSD sh, you need U0001f984 and it only works in UTF-8 locales.






share|improve this answer


















  • 1




    Most complete answer! Much appreciated, glad to see this!
    – Marcus Linsner
    Sep 6 at 11:11

















up vote
3
down vote













$ echo $'36023723020012'
😀


(that's bash's echo, GNU bash, version 4.3.43(1)-release (x86_64-redhat-linux-gnu))



Or you can use the echo binary:



$ /usr/bin/echo -e "36023723020012"
😀


How did I get it? I used maulinglawns's answer above to see the emoji's octal:



$ printf "U1F600n"
😀
$ printf "U1F600n"|hexdump -b
0000000 360 237 230 200 012
0000005


hexdump:
-b, --one-byte-octal one-byte octal display



EDIT: If you wanted the unicorn emoji:



$ echo $'360237246204'
🦄
$ `which echo` -e '360237246204'
🦄


If you want some generic way of getting the octal in that format:



printf "U1F600n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo
36023723020012


The output includes the n as 12. The ";echo" will add a newline at the end, it's useful for when trying it on command line, else the shell prompt will be shown right after the output.






share|improve this answer






















  • thanks for that function hexdump -b :)
    – intercoder
    Sep 6 at 14:17










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






intercoder is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467247%2fproblems-escaping-a-variable-when-using-echo%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
10
down vote



accepted










echo's syntax is different from the standard C escapes as supported by printf/awk/$'...'...



In standard echo syntax, you need a leading 0 in front of the octal sequence (which can have from 1 to 3 digits)¹:



echo '360237246204'


Note that for bash's echo builtin to work with that, you need to enable the xpg_echo option²:



$ UNICORN_utf8_printf_format='360237246204'
$ UNICORN_utf8_echo='360237246204'
$ UNICORN_utf8=$'360237246204'
$ printf "$UNICORN_utf8_printf_formatn"
🦄
$ printf '%sn' "$UNICORN_utf8"
🦄
$ shopt -s xpg_echo
$ echo "$UNICORN_utf8_echo"
🦄


Above, only $UNICORN_utf8 contains a 🦄 character, encoded in UTF8. The other ones contain sequences of backslash and digits that are meant to be expanded by the respective tools.



The %b format of the printf utility also understands the same sequences as echo. %b was actually added so we can get rid of echo which is impossible to use portably and reliably.



$ printf '%bn' "$UNICORN_utf8_echo"
🦄


See also (in zsh and bash³):



UNICORN_locale_encoding=$'U1f984'


Which gets you a Unicorn encoded in the locale's encoding, which would make it work even if the locale's encoding was not UTF-8 and also had that character (probably only GB18030, where 🦄 is encoded as $'2256033066' and where $'360237246204' would be the encoding of 馃 (NCJK UNIFIED IDEOGRAPH-9983N<private use area-E6E9>)).



Some printf implementations (including GNU printf and the printf builtin of zsh, ksh93 and recent versions of bash (4.2 or above)) also support those UXXXXXXXX escape sequences in their format argument (or arguments to %b except with ksh93); the GNU one needs 8 digits.




¹ GNU coreutils echo and busybox echo support ooo with -e as an extension (not when POSIXLY_CORRECT is in the environment for GNU echo)



² other option would be to use the non-standard -e option, but then it wouldn't work when both the posix and xpg_echo options are enabled, like when bash is in UNIX compliance mode.



³ ksh93 and mksh also support that syntax, but encode in UTF-8 regardless of the locale's encoding; in current (2018) versions of FreeBSD sh, you need U0001f984 and it only works in UTF-8 locales.






share|improve this answer


















  • 1




    Most complete answer! Much appreciated, glad to see this!
    – Marcus Linsner
    Sep 6 at 11:11














up vote
10
down vote



accepted










echo's syntax is different from the standard C escapes as supported by printf/awk/$'...'...



In standard echo syntax, you need a leading 0 in front of the octal sequence (which can have from 1 to 3 digits)¹:



echo '360237246204'


Note that for bash's echo builtin to work with that, you need to enable the xpg_echo option²:



$ UNICORN_utf8_printf_format='360237246204'
$ UNICORN_utf8_echo='360237246204'
$ UNICORN_utf8=$'360237246204'
$ printf "$UNICORN_utf8_printf_formatn"
🦄
$ printf '%sn' "$UNICORN_utf8"
🦄
$ shopt -s xpg_echo
$ echo "$UNICORN_utf8_echo"
🦄


Above, only $UNICORN_utf8 contains a 🦄 character, encoded in UTF8. The other ones contain sequences of backslash and digits that are meant to be expanded by the respective tools.



The %b format of the printf utility also understands the same sequences as echo. %b was actually added so we can get rid of echo which is impossible to use portably and reliably.



$ printf '%bn' "$UNICORN_utf8_echo"
🦄


See also (in zsh and bash³):



UNICORN_locale_encoding=$'U1f984'


Which gets you a Unicorn encoded in the locale's encoding, which would make it work even if the locale's encoding was not UTF-8 and also had that character (probably only GB18030, where 🦄 is encoded as $'2256033066' and where $'360237246204' would be the encoding of 馃 (NCJK UNIFIED IDEOGRAPH-9983N<private use area-E6E9>)).



Some printf implementations (including GNU printf and the printf builtin of zsh, ksh93 and recent versions of bash (4.2 or above)) also support those UXXXXXXXX escape sequences in their format argument (or arguments to %b except with ksh93); the GNU one needs 8 digits.




¹ GNU coreutils echo and busybox echo support ooo with -e as an extension (not when POSIXLY_CORRECT is in the environment for GNU echo)



² other option would be to use the non-standard -e option, but then it wouldn't work when both the posix and xpg_echo options are enabled, like when bash is in UNIX compliance mode.



³ ksh93 and mksh also support that syntax, but encode in UTF-8 regardless of the locale's encoding; in current (2018) versions of FreeBSD sh, you need U0001f984 and it only works in UTF-8 locales.






share|improve this answer


















  • 1




    Most complete answer! Much appreciated, glad to see this!
    – Marcus Linsner
    Sep 6 at 11:11












up vote
10
down vote



accepted







up vote
10
down vote



accepted






echo's syntax is different from the standard C escapes as supported by printf/awk/$'...'...



In standard echo syntax, you need a leading 0 in front of the octal sequence (which can have from 1 to 3 digits)¹:



echo '360237246204'


Note that for bash's echo builtin to work with that, you need to enable the xpg_echo option²:



$ UNICORN_utf8_printf_format='360237246204'
$ UNICORN_utf8_echo='360237246204'
$ UNICORN_utf8=$'360237246204'
$ printf "$UNICORN_utf8_printf_formatn"
🦄
$ printf '%sn' "$UNICORN_utf8"
🦄
$ shopt -s xpg_echo
$ echo "$UNICORN_utf8_echo"
🦄


Above, only $UNICORN_utf8 contains a 🦄 character, encoded in UTF8. The other ones contain sequences of backslash and digits that are meant to be expanded by the respective tools.



The %b format of the printf utility also understands the same sequences as echo. %b was actually added so we can get rid of echo which is impossible to use portably and reliably.



$ printf '%bn' "$UNICORN_utf8_echo"
🦄


See also (in zsh and bash³):



UNICORN_locale_encoding=$'U1f984'


Which gets you a Unicorn encoded in the locale's encoding, which would make it work even if the locale's encoding was not UTF-8 and also had that character (probably only GB18030, where 🦄 is encoded as $'2256033066' and where $'360237246204' would be the encoding of 馃 (NCJK UNIFIED IDEOGRAPH-9983N<private use area-E6E9>)).



Some printf implementations (including GNU printf and the printf builtin of zsh, ksh93 and recent versions of bash (4.2 or above)) also support those UXXXXXXXX escape sequences in their format argument (or arguments to %b except with ksh93); the GNU one needs 8 digits.




¹ GNU coreutils echo and busybox echo support ooo with -e as an extension (not when POSIXLY_CORRECT is in the environment for GNU echo)



² other option would be to use the non-standard -e option, but then it wouldn't work when both the posix and xpg_echo options are enabled, like when bash is in UNIX compliance mode.



³ ksh93 and mksh also support that syntax, but encode in UTF-8 regardless of the locale's encoding; in current (2018) versions of FreeBSD sh, you need U0001f984 and it only works in UTF-8 locales.






share|improve this answer














echo's syntax is different from the standard C escapes as supported by printf/awk/$'...'...



In standard echo syntax, you need a leading 0 in front of the octal sequence (which can have from 1 to 3 digits)¹:



echo '360237246204'


Note that for bash's echo builtin to work with that, you need to enable the xpg_echo option²:



$ UNICORN_utf8_printf_format='360237246204'
$ UNICORN_utf8_echo='360237246204'
$ UNICORN_utf8=$'360237246204'
$ printf "$UNICORN_utf8_printf_formatn"
🦄
$ printf '%sn' "$UNICORN_utf8"
🦄
$ shopt -s xpg_echo
$ echo "$UNICORN_utf8_echo"
🦄


Above, only $UNICORN_utf8 contains a 🦄 character, encoded in UTF8. The other ones contain sequences of backslash and digits that are meant to be expanded by the respective tools.



The %b format of the printf utility also understands the same sequences as echo. %b was actually added so we can get rid of echo which is impossible to use portably and reliably.



$ printf '%bn' "$UNICORN_utf8_echo"
🦄


See also (in zsh and bash³):



UNICORN_locale_encoding=$'U1f984'


Which gets you a Unicorn encoded in the locale's encoding, which would make it work even if the locale's encoding was not UTF-8 and also had that character (probably only GB18030, where 🦄 is encoded as $'2256033066' and where $'360237246204' would be the encoding of 馃 (NCJK UNIFIED IDEOGRAPH-9983N<private use area-E6E9>)).



Some printf implementations (including GNU printf and the printf builtin of zsh, ksh93 and recent versions of bash (4.2 or above)) also support those UXXXXXXXX escape sequences in their format argument (or arguments to %b except with ksh93); the GNU one needs 8 digits.




¹ GNU coreutils echo and busybox echo support ooo with -e as an extension (not when POSIXLY_CORRECT is in the environment for GNU echo)



² other option would be to use the non-standard -e option, but then it wouldn't work when both the posix and xpg_echo options are enabled, like when bash is in UNIX compliance mode.



³ ksh93 and mksh also support that syntax, but encode in UTF-8 regardless of the locale's encoding; in current (2018) versions of FreeBSD sh, you need U0001f984 and it only works in UTF-8 locales.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 6 at 12:16

























answered Sep 6 at 10:59









Stéphane Chazelas

283k53521858




283k53521858







  • 1




    Most complete answer! Much appreciated, glad to see this!
    – Marcus Linsner
    Sep 6 at 11:11












  • 1




    Most complete answer! Much appreciated, glad to see this!
    – Marcus Linsner
    Sep 6 at 11:11







1




1




Most complete answer! Much appreciated, glad to see this!
– Marcus Linsner
Sep 6 at 11:11




Most complete answer! Much appreciated, glad to see this!
– Marcus Linsner
Sep 6 at 11:11












up vote
3
down vote













$ echo $'36023723020012'
😀


(that's bash's echo, GNU bash, version 4.3.43(1)-release (x86_64-redhat-linux-gnu))



Or you can use the echo binary:



$ /usr/bin/echo -e "36023723020012"
😀


How did I get it? I used maulinglawns's answer above to see the emoji's octal:



$ printf "U1F600n"
😀
$ printf "U1F600n"|hexdump -b
0000000 360 237 230 200 012
0000005


hexdump:
-b, --one-byte-octal one-byte octal display



EDIT: If you wanted the unicorn emoji:



$ echo $'360237246204'
🦄
$ `which echo` -e '360237246204'
🦄


If you want some generic way of getting the octal in that format:



printf "U1F600n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo
36023723020012


The output includes the n as 12. The ";echo" will add a newline at the end, it's useful for when trying it on command line, else the shell prompt will be shown right after the output.






share|improve this answer






















  • thanks for that function hexdump -b :)
    – intercoder
    Sep 6 at 14:17














up vote
3
down vote













$ echo $'36023723020012'
😀


(that's bash's echo, GNU bash, version 4.3.43(1)-release (x86_64-redhat-linux-gnu))



Or you can use the echo binary:



$ /usr/bin/echo -e "36023723020012"
😀


How did I get it? I used maulinglawns's answer above to see the emoji's octal:



$ printf "U1F600n"
😀
$ printf "U1F600n"|hexdump -b
0000000 360 237 230 200 012
0000005


hexdump:
-b, --one-byte-octal one-byte octal display



EDIT: If you wanted the unicorn emoji:



$ echo $'360237246204'
🦄
$ `which echo` -e '360237246204'
🦄


If you want some generic way of getting the octal in that format:



printf "U1F600n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo
36023723020012


The output includes the n as 12. The ";echo" will add a newline at the end, it's useful for when trying it on command line, else the shell prompt will be shown right after the output.






share|improve this answer






















  • thanks for that function hexdump -b :)
    – intercoder
    Sep 6 at 14:17












up vote
3
down vote










up vote
3
down vote









$ echo $'36023723020012'
😀


(that's bash's echo, GNU bash, version 4.3.43(1)-release (x86_64-redhat-linux-gnu))



Or you can use the echo binary:



$ /usr/bin/echo -e "36023723020012"
😀


How did I get it? I used maulinglawns's answer above to see the emoji's octal:



$ printf "U1F600n"
😀
$ printf "U1F600n"|hexdump -b
0000000 360 237 230 200 012
0000005


hexdump:
-b, --one-byte-octal one-byte octal display



EDIT: If you wanted the unicorn emoji:



$ echo $'360237246204'
🦄
$ `which echo` -e '360237246204'
🦄


If you want some generic way of getting the octal in that format:



printf "U1F600n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo
36023723020012


The output includes the n as 12. The ";echo" will add a newline at the end, it's useful for when trying it on command line, else the shell prompt will be shown right after the output.






share|improve this answer














$ echo $'36023723020012'
😀


(that's bash's echo, GNU bash, version 4.3.43(1)-release (x86_64-redhat-linux-gnu))



Or you can use the echo binary:



$ /usr/bin/echo -e "36023723020012"
😀


How did I get it? I used maulinglawns's answer above to see the emoji's octal:



$ printf "U1F600n"
😀
$ printf "U1F600n"|hexdump -b
0000000 360 237 230 200 012
0000005


hexdump:
-b, --one-byte-octal one-byte octal display



EDIT: If you wanted the unicorn emoji:



$ echo $'360237246204'
🦄
$ `which echo` -e '360237246204'
🦄


If you want some generic way of getting the octal in that format:



printf "U1F600n"|hexdump -v -e '"\" 1/1 "%03o"' ; echo
36023723020012


The output includes the n as 12. The ";echo" will add a newline at the end, it's useful for when trying it on command line, else the shell prompt will be shown right after the output.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 6 at 15:02

























answered Sep 6 at 10:53









Marcus Linsner

10914




10914











  • thanks for that function hexdump -b :)
    – intercoder
    Sep 6 at 14:17
















  • thanks for that function hexdump -b :)
    – intercoder
    Sep 6 at 14:17















thanks for that function hexdump -b :)
– intercoder
Sep 6 at 14:17




thanks for that function hexdump -b :)
– intercoder
Sep 6 at 14:17










intercoder is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















intercoder is a new contributor. Be nice, and check out our Code of Conduct.












intercoder is a new contributor. Be nice, and check out our Code of Conduct.











intercoder is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467247%2fproblems-escaping-a-variable-when-using-echo%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke