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
New contributor
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
New contributor
Possibly related: Echo hash only from shasum
– steeldriver
15 hours ago
@steeldriver I'm not asking how to calculate hash
– Immani
14 hours ago
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
14 hours ago
@steeldriver yes, but your link suggests perl
– Immani
14 hours ago
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
New contributor
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
New contributor
New contributor
New contributor
asked 15 hours ago
ImmaniImmani
1
1
New contributor
New contributor
Possibly related: Echo hash only from shasum
– steeldriver
15 hours ago
@steeldriver I'm not asking how to calculate hash
– Immani
14 hours ago
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
14 hours ago
@steeldriver yes, but your link suggests perl
– Immani
14 hours ago
add a comment |
Possibly related: Echo hash only from shasum
– steeldriver
15 hours ago
@steeldriver I'm not asking how to calculate hash
– Immani
14 hours ago
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
14 hours ago
@steeldriver yes, but your link suggests perl
– Immani
14 hours ago
Possibly related: Echo hash only from shasum
– steeldriver
15 hours ago
Possibly related: Echo hash only from shasum
– steeldriver
15 hours ago
@steeldriver I'm not asking how to calculate hash
– Immani
14 hours ago
@steeldriver I'm not asking how to calculate hash
– Immani
14 hours ago
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
14 hours ago
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
14 hours ago
@steeldriver yes, but your link suggests perl
– Immani
14 hours ago
@steeldriver yes, but your link suggests perl
– Immani
14 hours ago
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
14 hours ago
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@Immani, edited
– ilkkachu
12 hours ago
what are>
,n
and<
for?
– Immani
11 hours ago
|
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
});
}
});
Immani is a new contributor. Be nice, and check out our Code of Conduct.
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
14 hours ago
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@Immani, edited
– ilkkachu
12 hours ago
what are>
,n
and<
for?
– Immani
11 hours ago
|
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
14 hours ago
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@Immani, edited
– ilkkachu
12 hours ago
what are>
,n
and<
for?
– Immani
11 hours ago
|
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 12 hours ago
answered 14 hours ago
ilkkachuilkkachu
56.7k784156
56.7k784156
how did those 2 whitespaces and the dash end up in the variable?
– Immani
14 hours ago
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@Immani, edited
– ilkkachu
12 hours ago
what are>
,n
and<
for?
– Immani
11 hours ago
|
show 1 more comment
how did those 2 whitespaces and the dash end up in the variable?
– Immani
14 hours ago
@Immani because that's the default behavior whenshasum
reads from standard input instead of a named file
– steeldriver
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@Immani, edited
– ilkkachu
12 hours ago
what are>
,n
and<
for?
– Immani
11 hours ago
how did those 2 whitespaces and the dash end up in the variable?
– Immani
14 hours ago
how did those 2 whitespaces and the dash end up in the variable?
– Immani
14 hours ago
@Immani because that's the default behavior when
shasum
reads from standard input instead of a named file– steeldriver
14 hours ago
@Immani because that's the default behavior when
shasum
reads from standard input instead of a named file– steeldriver
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@steeldriver how to get rid of them?
– Immani
14 hours ago
@Immani, edited
– ilkkachu
12 hours ago
@Immani, edited
– ilkkachu
12 hours ago
what are
>
, n
and <
for?– Immani
11 hours ago
what are
>
, n
and <
for?– Immani
11 hours ago
|
show 1 more comment
Immani is a new contributor. Be nice, and check out our Code of Conduct.
Immani is a new contributor. Be nice, and check out our Code of Conduct.
Immani is a new contributor. Be nice, and check out our Code of Conduct.
Immani is a new contributor. Be nice, and check out our Code of Conduct.
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
15 hours ago
@steeldriver I'm not asking how to calculate hash
– Immani
14 hours ago
But you do seem to be asking why the result has three extra characters, and how to remove them
– steeldriver
14 hours ago
@steeldriver yes, but your link suggests perl
– Immani
14 hours ago