Visible content of 2 variables in bash is the same, but the length is different
These 2 variables will have the same visible content
x_sign1="aabbccdd_and_somthing_else"
var1="...."
[........]
x_sign2=$(echo -n "${var1}${var2}${var3}" | shasum -a 256)
echo $x_sign2
====>
aabbccdd_and_somthing_else -
Note the "-" in the end.
However, their lengths will be different. Even though the x_sign2
doesn't contain a new line symbol. To ensure this:
x_sign22=$(echo -n "${var1}${var2}${var3}" | shasum -a 256 | tr -d 'n')
But:
echo ${#x_sign1}
====> 64
And:
And:
echo ${#x_sign2}
====> 67
echo ${#x_sign22}
====> 67
The difference is 3 symbols. The visible content is identical.
Also, when I make a request via curl to a REST API which needs that value of a signature, x_sign1
always succeeds, whereas x_sign2
doesn't -- "wrong signature"
Why? How to fix that?
bash terminal
add a comment |
These 2 variables will have the same visible content
x_sign1="aabbccdd_and_somthing_else"
var1="...."
[........]
x_sign2=$(echo -n "${var1}${var2}${var3}" | shasum -a 256)
echo $x_sign2
====>
aabbccdd_and_somthing_else -
Note the "-" in the end.
However, their lengths will be different. Even though the x_sign2
doesn't contain a new line symbol. To ensure this:
x_sign22=$(echo -n "${var1}${var2}${var3}" | shasum -a 256 | tr -d 'n')
But:
echo ${#x_sign1}
====> 64
And:
And:
echo ${#x_sign2}
====> 67
echo ${#x_sign22}
====> 67
The difference is 3 symbols. The visible content is identical.
Also, when I make a request via curl to a REST API which needs that value of a signature, x_sign1
always succeeds, whereas x_sign2
doesn't -- "wrong signature"
Why? How to fix that?
bash terminal
Possibly related: Echo hash only from shasum
– steeldriver
Jan 17 at 15:55
@steeldriver I'm not asking how to calculate hash
– Immani
Jan 17 at 16:14
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
Jan 17 at 16:28
@steeldriver yes, but your link suggests perl
– Immani
Jan 17 at 16:34
add a comment |
These 2 variables will have the same visible content
x_sign1="aabbccdd_and_somthing_else"
var1="...."
[........]
x_sign2=$(echo -n "${var1}${var2}${var3}" | shasum -a 256)
echo $x_sign2
====>
aabbccdd_and_somthing_else -
Note the "-" in the end.
However, their lengths will be different. Even though the x_sign2
doesn't contain a new line symbol. To ensure this:
x_sign22=$(echo -n "${var1}${var2}${var3}" | shasum -a 256 | tr -d 'n')
But:
echo ${#x_sign1}
====> 64
And:
And:
echo ${#x_sign2}
====> 67
echo ${#x_sign22}
====> 67
The difference is 3 symbols. The visible content is identical.
Also, when I make a request via curl to a REST API which needs that value of a signature, x_sign1
always succeeds, whereas x_sign2
doesn't -- "wrong signature"
Why? How to fix that?
bash terminal
These 2 variables will have the same visible content
x_sign1="aabbccdd_and_somthing_else"
var1="...."
[........]
x_sign2=$(echo -n "${var1}${var2}${var3}" | shasum -a 256)
echo $x_sign2
====>
aabbccdd_and_somthing_else -
Note the "-" in the end.
However, their lengths will be different. Even though the x_sign2
doesn't contain a new line symbol. To ensure this:
x_sign22=$(echo -n "${var1}${var2}${var3}" | shasum -a 256 | tr -d 'n')
But:
echo ${#x_sign1}
====> 64
And:
And:
echo ${#x_sign2}
====> 67
echo ${#x_sign22}
====> 67
The difference is 3 symbols. The visible content is identical.
Also, when I make a request via curl to a REST API which needs that value of a signature, x_sign1
always succeeds, whereas x_sign2
doesn't -- "wrong signature"
Why? How to fix that?
bash terminal
bash terminal
asked Jan 17 at 15:50
ImmaniImmani
1
1
Possibly related: Echo hash only from shasum
– steeldriver
Jan 17 at 15:55
@steeldriver I'm not asking how to calculate hash
– Immani
Jan 17 at 16:14
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
Jan 17 at 16:28
@steeldriver yes, but your link suggests perl
– Immani
Jan 17 at 16:34
add a comment |
Possibly related: Echo hash only from shasum
– steeldriver
Jan 17 at 15:55
@steeldriver I'm not asking how to calculate hash
– Immani
Jan 17 at 16:14
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
Jan 17 at 16:28
@steeldriver yes, but your link suggests perl
– Immani
Jan 17 at 16:34
Possibly related: Echo hash only from shasum
– steeldriver
Jan 17 at 15:55
Possibly related: Echo hash only from shasum
– steeldriver
Jan 17 at 15:55
@steeldriver I'm not asking how to calculate hash
– Immani
Jan 17 at 16:14
@steeldriver I'm not asking how to calculate hash
– Immani
Jan 17 at 16:14
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
Jan 17 at 16:28
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
Jan 17 at 16:28
@steeldriver yes, but your link suggests perl
– Immani
Jan 17 at 16:34
@steeldriver yes, but your link suggests perl
– Immani
Jan 17 at 16:34
add a comment |
1 Answer
1
active
oldest
votes
$ echo foo |shasum -a 256
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c -
^^
Note that there are two spaces in the output of shasum
before the filename. When the input is taken from stdin, shasum
prints a dash as the filename.
If you run echo foo | shasum | od -c
you can check that, and see the newline at the end also. The newline, however, is removed by the command substitution, so removing it explicitly with tr
doesn't do anything. (see here and here)
The two spaces and the dash are three characters that cause the difference in your counts.
To get just the hash, you could use parameter expansions to remove anything after the first space, e.g.:
$ h=$(echo foo | shasum -a 256)
$ h=${h%% *}
$ printf ">%s<n" "$h"
>b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c<
The ${var%%pattern}
expands to the value of var
with the longest suffix matching pattern
removed.
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
Jan 17 at 16:27
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@Immani, edited
– ilkkachu
Jan 17 at 18:16
what are>
,n
and<
for?
– Immani
Jan 17 at 19:41
|
show 1 more comment
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495098%2fvisible-content-of-2-variables-in-bash-is-the-same-but-the-length-is-different%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$ echo foo |shasum -a 256
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c -
^^
Note that there are two spaces in the output of shasum
before the filename. When the input is taken from stdin, shasum
prints a dash as the filename.
If you run echo foo | shasum | od -c
you can check that, and see the newline at the end also. The newline, however, is removed by the command substitution, so removing it explicitly with tr
doesn't do anything. (see here and here)
The two spaces and the dash are three characters that cause the difference in your counts.
To get just the hash, you could use parameter expansions to remove anything after the first space, e.g.:
$ h=$(echo foo | shasum -a 256)
$ h=${h%% *}
$ printf ">%s<n" "$h"
>b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c<
The ${var%%pattern}
expands to the value of var
with the longest suffix matching pattern
removed.
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
Jan 17 at 16:27
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@Immani, edited
– ilkkachu
Jan 17 at 18:16
what are>
,n
and<
for?
– Immani
Jan 17 at 19:41
|
show 1 more comment
$ echo foo |shasum -a 256
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c -
^^
Note that there are two spaces in the output of shasum
before the filename. When the input is taken from stdin, shasum
prints a dash as the filename.
If you run echo foo | shasum | od -c
you can check that, and see the newline at the end also. The newline, however, is removed by the command substitution, so removing it explicitly with tr
doesn't do anything. (see here and here)
The two spaces and the dash are three characters that cause the difference in your counts.
To get just the hash, you could use parameter expansions to remove anything after the first space, e.g.:
$ h=$(echo foo | shasum -a 256)
$ h=${h%% *}
$ printf ">%s<n" "$h"
>b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c<
The ${var%%pattern}
expands to the value of var
with the longest suffix matching pattern
removed.
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
Jan 17 at 16:27
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@Immani, edited
– ilkkachu
Jan 17 at 18:16
what are>
,n
and<
for?
– Immani
Jan 17 at 19:41
|
show 1 more comment
$ echo foo |shasum -a 256
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c -
^^
Note that there are two spaces in the output of shasum
before the filename. When the input is taken from stdin, shasum
prints a dash as the filename.
If you run echo foo | shasum | od -c
you can check that, and see the newline at the end also. The newline, however, is removed by the command substitution, so removing it explicitly with tr
doesn't do anything. (see here and here)
The two spaces and the dash are three characters that cause the difference in your counts.
To get just the hash, you could use parameter expansions to remove anything after the first space, e.g.:
$ h=$(echo foo | shasum -a 256)
$ h=${h%% *}
$ printf ">%s<n" "$h"
>b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c<
The ${var%%pattern}
expands to the value of var
with the longest suffix matching pattern
removed.
$ echo foo |shasum -a 256
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c -
^^
Note that there are two spaces in the output of shasum
before the filename. When the input is taken from stdin, shasum
prints a dash as the filename.
If you run echo foo | shasum | od -c
you can check that, and see the newline at the end also. The newline, however, is removed by the command substitution, so removing it explicitly with tr
doesn't do anything. (see here and here)
The two spaces and the dash are three characters that cause the difference in your counts.
To get just the hash, you could use parameter expansions to remove anything after the first space, e.g.:
$ h=$(echo foo | shasum -a 256)
$ h=${h%% *}
$ printf ">%s<n" "$h"
>b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c<
The ${var%%pattern}
expands to the value of var
with the longest suffix matching pattern
removed.
edited Jan 17 at 18:14
answered Jan 17 at 16:07
ilkkachuilkkachu
57k785158
57k785158
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
Jan 17 at 16:27
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@Immani, edited
– ilkkachu
Jan 17 at 18:16
what are>
,n
and<
for?
– Immani
Jan 17 at 19:41
|
show 1 more comment
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
Jan 17 at 16:27
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@Immani, edited
– ilkkachu
Jan 17 at 18:16
what are>
,n
and<
for?
– Immani
Jan 17 at 19:41
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
how did those 2 whitespaces and the dash end up in the variable?
– Immani
Jan 17 at 16:13
@Immani because that's the default behavior when
shasum
reads from standard input instead of a named file– steeldriver
Jan 17 at 16:27
@Immani because that's the default behavior when
shasum
reads from standard input instead of a named file– steeldriver
Jan 17 at 16:27
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@steeldriver how to get rid of them?
– Immani
Jan 17 at 16:33
@Immani, edited
– ilkkachu
Jan 17 at 18:16
@Immani, edited
– ilkkachu
Jan 17 at 18:16
what are
>
, n
and <
for?– Immani
Jan 17 at 19:41
what are
>
, n
and <
for?– Immani
Jan 17 at 19:41
|
show 1 more comment
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495098%2fvisible-content-of-2-variables-in-bash-is-the-same-but-the-length-is-different%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possibly related: Echo hash only from shasum
– steeldriver
Jan 17 at 15:55
@steeldriver I'm not asking how to calculate hash
– Immani
Jan 17 at 16:14
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
Jan 17 at 16:28
@steeldriver yes, but your link suggests perl
– Immani
Jan 17 at 16:34