bash command to print string in unambiguous form

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











up vote
5
down vote

favorite
1












I am wondering whether there exists a command in bash to print a string in a way exposes the special character it contains.



For example, suppose that a=$'abe'; does there exists a function to print abe literally from $a?



The closest I have got so far is by using the l command from sed:



echo "$a" | sed -n 'l'


which returns a00b33$, but the notation is different from that inside $'', and it doesn't work if the string contains newlines.







share|improve this question


















  • 1




    As variant the cat -A. It shows non-printable characters, but in the another form. echo $'tbea' | cat -A output: ^I^H^[^G
    – MiniMax
    Oct 15 '17 at 20:35






  • 1




    Also, od -c: echo $'tbea' | od -c. Output: 0000000 t b 033 a n.
    – MiniMax
    Oct 15 '17 at 20:50







  • 1




    A slightly closer option is printf %s "$a" | hexdump -c, which gives me … a b 033 …, i.e. just mangles the e.
    – Sparhawk
    Oct 15 '17 at 21:16






  • 1




    Can a string contain ? I don't think so. @Sparhawk, what is your bash version?
    – glenn jackman
    Oct 16 '17 at 0:48







  • 1




    zsh is not bash. It's a completely different shell
    – glenn jackman
    Oct 16 '17 at 9:50














up vote
5
down vote

favorite
1












I am wondering whether there exists a command in bash to print a string in a way exposes the special character it contains.



For example, suppose that a=$'abe'; does there exists a function to print abe literally from $a?



The closest I have got so far is by using the l command from sed:



echo "$a" | sed -n 'l'


which returns a00b33$, but the notation is different from that inside $'', and it doesn't work if the string contains newlines.







share|improve this question


















  • 1




    As variant the cat -A. It shows non-printable characters, but in the another form. echo $'tbea' | cat -A output: ^I^H^[^G
    – MiniMax
    Oct 15 '17 at 20:35






  • 1




    Also, od -c: echo $'tbea' | od -c. Output: 0000000 t b 033 a n.
    – MiniMax
    Oct 15 '17 at 20:50







  • 1




    A slightly closer option is printf %s "$a" | hexdump -c, which gives me … a b 033 …, i.e. just mangles the e.
    – Sparhawk
    Oct 15 '17 at 21:16






  • 1




    Can a string contain ? I don't think so. @Sparhawk, what is your bash version?
    – glenn jackman
    Oct 16 '17 at 0:48







  • 1




    zsh is not bash. It's a completely different shell
    – glenn jackman
    Oct 16 '17 at 9:50












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





I am wondering whether there exists a command in bash to print a string in a way exposes the special character it contains.



For example, suppose that a=$'abe'; does there exists a function to print abe literally from $a?



The closest I have got so far is by using the l command from sed:



echo "$a" | sed -n 'l'


which returns a00b33$, but the notation is different from that inside $'', and it doesn't work if the string contains newlines.







share|improve this question














I am wondering whether there exists a command in bash to print a string in a way exposes the special character it contains.



For example, suppose that a=$'abe'; does there exists a function to print abe literally from $a?



The closest I have got so far is by using the l command from sed:



echo "$a" | sed -n 'l'


which returns a00b33$, but the notation is different from that inside $'', and it doesn't work if the string contains newlines.









share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 15:09

























asked Oct 15 '17 at 20:18









Rastapopoulos

518112




518112







  • 1




    As variant the cat -A. It shows non-printable characters, but in the another form. echo $'tbea' | cat -A output: ^I^H^[^G
    – MiniMax
    Oct 15 '17 at 20:35






  • 1




    Also, od -c: echo $'tbea' | od -c. Output: 0000000 t b 033 a n.
    – MiniMax
    Oct 15 '17 at 20:50







  • 1




    A slightly closer option is printf %s "$a" | hexdump -c, which gives me … a b 033 …, i.e. just mangles the e.
    – Sparhawk
    Oct 15 '17 at 21:16






  • 1




    Can a string contain ? I don't think so. @Sparhawk, what is your bash version?
    – glenn jackman
    Oct 16 '17 at 0:48







  • 1




    zsh is not bash. It's a completely different shell
    – glenn jackman
    Oct 16 '17 at 9:50












  • 1




    As variant the cat -A. It shows non-printable characters, but in the another form. echo $'tbea' | cat -A output: ^I^H^[^G
    – MiniMax
    Oct 15 '17 at 20:35






  • 1




    Also, od -c: echo $'tbea' | od -c. Output: 0000000 t b 033 a n.
    – MiniMax
    Oct 15 '17 at 20:50







  • 1




    A slightly closer option is printf %s "$a" | hexdump -c, which gives me … a b 033 …, i.e. just mangles the e.
    – Sparhawk
    Oct 15 '17 at 21:16






  • 1




    Can a string contain ? I don't think so. @Sparhawk, what is your bash version?
    – glenn jackman
    Oct 16 '17 at 0:48







  • 1




    zsh is not bash. It's a completely different shell
    – glenn jackman
    Oct 16 '17 at 9:50







1




1




As variant the cat -A. It shows non-printable characters, but in the another form. echo $'tbea' | cat -A output: ^I^H^[^G
– MiniMax
Oct 15 '17 at 20:35




As variant the cat -A. It shows non-printable characters, but in the another form. echo $'tbea' | cat -A output: ^I^H^[^G
– MiniMax
Oct 15 '17 at 20:35




1




1




Also, od -c: echo $'tbea' | od -c. Output: 0000000 t b 033 a n.
– MiniMax
Oct 15 '17 at 20:50





Also, od -c: echo $'tbea' | od -c. Output: 0000000 t b 033 a n.
– MiniMax
Oct 15 '17 at 20:50





1




1




A slightly closer option is printf %s "$a" | hexdump -c, which gives me … a b 033 …, i.e. just mangles the e.
– Sparhawk
Oct 15 '17 at 21:16




A slightly closer option is printf %s "$a" | hexdump -c, which gives me … a b 033 …, i.e. just mangles the e.
– Sparhawk
Oct 15 '17 at 21:16




1




1




Can a string contain ? I don't think so. @Sparhawk, what is your bash version?
– glenn jackman
Oct 16 '17 at 0:48





Can a string contain ? I don't think so. @Sparhawk, what is your bash version?
– glenn jackman
Oct 16 '17 at 0:48





1




1




zsh is not bash. It's a completely different shell
– glenn jackman
Oct 16 '17 at 9:50




zsh is not bash. It's a completely different shell
– glenn jackman
Oct 16 '17 at 9:50










1 Answer
1






active

oldest

votes

















up vote
6
down vote













var=$'a b 10 c'
printf %q "$var"
$'a b b c'


This works in bash. I do not know how compatible this is.






share|improve this answer
















  • 2




    In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
    – chepner
    Oct 16 '17 at 1:47










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
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398278%2fbash-command-to-print-string-in-unambiguous-form%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote













var=$'a b 10 c'
printf %q "$var"
$'a b b c'


This works in bash. I do not know how compatible this is.






share|improve this answer
















  • 2




    In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
    – chepner
    Oct 16 '17 at 1:47














up vote
6
down vote













var=$'a b 10 c'
printf %q "$var"
$'a b b c'


This works in bash. I do not know how compatible this is.






share|improve this answer
















  • 2




    In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
    – chepner
    Oct 16 '17 at 1:47












up vote
6
down vote










up vote
6
down vote









var=$'a b 10 c'
printf %q "$var"
$'a b b c'


This works in bash. I do not know how compatible this is.






share|improve this answer












var=$'a b 10 c'
printf %q "$var"
$'a b b c'


This works in bash. I do not know how compatible this is.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 15 '17 at 21:31









Hauke Laging

53.6k1282130




53.6k1282130







  • 2




    In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
    – chepner
    Oct 16 '17 at 1:47












  • 2




    In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
    – chepner
    Oct 16 '17 at 1:47







2




2




In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
– chepner
Oct 16 '17 at 1:47




In bash 4.4, you can also get the same string via parameter expansion: $a@Q.
– chepner
Oct 16 '17 at 1:47

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398278%2fbash-command-to-print-string-in-unambiguous-form%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?