bash command to print string in unambiguous form
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
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.
bash string
 |Â
show 1 more comment
up vote
5
down vote
favorite
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.
bash string
1
As variant thecat -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 isprintf %s "$a" | hexdump -c
, which gives me⦠a b 033 â¦
, i.e. just mangles thee
.
â 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
 |Â
show 1 more comment
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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.
bash string
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.
bash string
edited Jan 14 at 15:09
asked Oct 15 '17 at 20:18
Rastapopoulos
518112
518112
1
As variant thecat -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 isprintf %s "$a" | hexdump -c
, which gives me⦠a b 033 â¦
, i.e. just mangles thee
.
â 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
 |Â
show 1 more comment
1
As variant thecat -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 isprintf %s "$a" | hexdump -c
, which gives me⦠a b 033 â¦
, i.e. just mangles thee
.
â 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
 |Â
show 1 more comment
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.
2
Inbash
4.4, you can also get the same string via parameter expansion:$a@Q
.
â chepner
Oct 16 '17 at 1:47
add a comment |Â
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.
2
Inbash
4.4, you can also get the same string via parameter expansion:$a@Q
.
â chepner
Oct 16 '17 at 1:47
add a comment |Â
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.
2
Inbash
4.4, you can also get the same string via parameter expansion:$a@Q
.
â chepner
Oct 16 '17 at 1:47
add a comment |Â
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.
var=$'a b 10 c'
printf %q "$var"
$'a b b c'
This works in bash
. I do not know how compatible this is.
answered Oct 15 '17 at 21:31
Hauke Laging
53.6k1282130
53.6k1282130
2
Inbash
4.4, you can also get the same string via parameter expansion:$a@Q
.
â chepner
Oct 16 '17 at 1:47
add a comment |Â
2
Inbash
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 thee
.â 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