Visible content of 2 variables in bash is the same, but the length is different












0















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?










share|improve this question







New contributor




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





















  • 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
















0















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?










share|improve this question







New contributor




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





















  • 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














0












0








0








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?










share|improve this question







New contributor




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












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






share|improve this question







New contributor




Immani 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







New contributor




Immani 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






New contributor




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









asked 15 hours ago









ImmaniImmani

1




1




New contributor




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





New contributor





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






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













  • 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











  • @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










1 Answer
1






active

oldest

votes


















2














$ 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.






share|improve this answer


























  • 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











  • @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













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.










draft saved

draft discarded


















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









2














$ 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.






share|improve this answer


























  • 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











  • @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


















2














$ 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.






share|improve this answer


























  • 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











  • @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
















2












2








2







$ 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.






share|improve this answer















$ 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.







share|improve this answer














share|improve this answer



share|improve this answer








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











  • @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











  • @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











  • @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












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










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Loup dans la culture

How to solve the problem of ntp “Unable to contact time server” from KDE?

ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux