In a bash script, how to use a variable inside a command and assign to a new variable?

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












0















I'm trying to store the MD5 of a variable into another variable. Between backticks and the more modern () notation, I cannot figure out how to assign the value of a variable run through a command to another variable. Sample code:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$( $backup_dir | md5)


Here, the hashed variable doesn't work, it takes the literal string backup_dir and hashes that. So the hash is always the same. Any thoughts?



Thanks!










share|improve this question
























  • Did you mean hashed=$( echo $backup_dir | md5) ?

    – Stephen Harris
    Feb 18 at 15:00















0















I'm trying to store the MD5 of a variable into another variable. Between backticks and the more modern () notation, I cannot figure out how to assign the value of a variable run through a command to another variable. Sample code:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$( $backup_dir | md5)


Here, the hashed variable doesn't work, it takes the literal string backup_dir and hashes that. So the hash is always the same. Any thoughts?



Thanks!










share|improve this question
























  • Did you mean hashed=$( echo $backup_dir | md5) ?

    – Stephen Harris
    Feb 18 at 15:00













0












0








0








I'm trying to store the MD5 of a variable into another variable. Between backticks and the more modern () notation, I cannot figure out how to assign the value of a variable run through a command to another variable. Sample code:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$( $backup_dir | md5)


Here, the hashed variable doesn't work, it takes the literal string backup_dir and hashes that. So the hash is always the same. Any thoughts?



Thanks!










share|improve this question
















I'm trying to store the MD5 of a variable into another variable. Between backticks and the more modern () notation, I cannot figure out how to assign the value of a variable run through a command to another variable. Sample code:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$( $backup_dir | md5)


Here, the hashed variable doesn't work, it takes the literal string backup_dir and hashes that. So the hash is always the same. Any thoughts?



Thanks!







bash shell-script hashsum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 18 at 16:33









Kusalananda

135k17255422




135k17255422










asked Feb 18 at 14:55









Khom NazidKhom Nazid

31




31












  • Did you mean hashed=$( echo $backup_dir | md5) ?

    – Stephen Harris
    Feb 18 at 15:00

















  • Did you mean hashed=$( echo $backup_dir | md5) ?

    – Stephen Harris
    Feb 18 at 15:00
















Did you mean hashed=$( echo $backup_dir | md5) ?

– Stephen Harris
Feb 18 at 15:00





Did you mean hashed=$( echo $backup_dir | md5) ?

– Stephen Harris
Feb 18 at 15:00










1 Answer
1






active

oldest

votes


















3














You are expecting md5 to read the value of the backup_dir variable and return its MD5 hash sum.



The command pipeline



$backup_dir | md5


would try to run $backup_dir as a command, piping its output to md5. I would expect a "command not found" error from this, along with the MD5 hash of the empty string (d41d8cd98f00b204e9800998ecf8427e) in $hashed.



Instead, you would need to use something like



printf '%s' "$backup_dir" | md5


to give md5 the value on its standard input stream.



You could also use echo "$backup_dir" | md5 or md5 <<<"$backup_dir", but note that this adds a newline to the end of the value of $backup_dir which would alter the hash.



If md5 is the md5 utility commonly found on BSD and BSD-like systems (e.g. macOS), then you should use



md5 -q -s "$backup_dir"


The -s option takes a string as its argument, and -q causes md5 to only print out the hash of that string and nothing else.



Summary:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$(md5 -q -s "$backup_dir")





share|improve this answer




















  • 1





    This works and looks clean. Thank you for the patient explanation!

    – Khom Nazid
    Feb 18 at 16:01










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



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501377%2fin-a-bash-script-how-to-use-a-variable-inside-a-command-and-assign-to-a-new-var%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









3














You are expecting md5 to read the value of the backup_dir variable and return its MD5 hash sum.



The command pipeline



$backup_dir | md5


would try to run $backup_dir as a command, piping its output to md5. I would expect a "command not found" error from this, along with the MD5 hash of the empty string (d41d8cd98f00b204e9800998ecf8427e) in $hashed.



Instead, you would need to use something like



printf '%s' "$backup_dir" | md5


to give md5 the value on its standard input stream.



You could also use echo "$backup_dir" | md5 or md5 <<<"$backup_dir", but note that this adds a newline to the end of the value of $backup_dir which would alter the hash.



If md5 is the md5 utility commonly found on BSD and BSD-like systems (e.g. macOS), then you should use



md5 -q -s "$backup_dir"


The -s option takes a string as its argument, and -q causes md5 to only print out the hash of that string and nothing else.



Summary:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$(md5 -q -s "$backup_dir")





share|improve this answer




















  • 1





    This works and looks clean. Thank you for the patient explanation!

    – Khom Nazid
    Feb 18 at 16:01















3














You are expecting md5 to read the value of the backup_dir variable and return its MD5 hash sum.



The command pipeline



$backup_dir | md5


would try to run $backup_dir as a command, piping its output to md5. I would expect a "command not found" error from this, along with the MD5 hash of the empty string (d41d8cd98f00b204e9800998ecf8427e) in $hashed.



Instead, you would need to use something like



printf '%s' "$backup_dir" | md5


to give md5 the value on its standard input stream.



You could also use echo "$backup_dir" | md5 or md5 <<<"$backup_dir", but note that this adds a newline to the end of the value of $backup_dir which would alter the hash.



If md5 is the md5 utility commonly found on BSD and BSD-like systems (e.g. macOS), then you should use



md5 -q -s "$backup_dir"


The -s option takes a string as its argument, and -q causes md5 to only print out the hash of that string and nothing else.



Summary:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$(md5 -q -s "$backup_dir")





share|improve this answer




















  • 1





    This works and looks clean. Thank you for the patient explanation!

    – Khom Nazid
    Feb 18 at 16:01













3












3








3







You are expecting md5 to read the value of the backup_dir variable and return its MD5 hash sum.



The command pipeline



$backup_dir | md5


would try to run $backup_dir as a command, piping its output to md5. I would expect a "command not found" error from this, along with the MD5 hash of the empty string (d41d8cd98f00b204e9800998ecf8427e) in $hashed.



Instead, you would need to use something like



printf '%s' "$backup_dir" | md5


to give md5 the value on its standard input stream.



You could also use echo "$backup_dir" | md5 or md5 <<<"$backup_dir", but note that this adds a newline to the end of the value of $backup_dir which would alter the hash.



If md5 is the md5 utility commonly found on BSD and BSD-like systems (e.g. macOS), then you should use



md5 -q -s "$backup_dir"


The -s option takes a string as its argument, and -q causes md5 to only print out the hash of that string and nothing else.



Summary:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$(md5 -q -s "$backup_dir")





share|improve this answer















You are expecting md5 to read the value of the backup_dir variable and return its MD5 hash sum.



The command pipeline



$backup_dir | md5


would try to run $backup_dir as a command, piping its output to md5. I would expect a "command not found" error from this, along with the MD5 hash of the empty string (d41d8cd98f00b204e9800998ecf8427e) in $hashed.



Instead, you would need to use something like



printf '%s' "$backup_dir" | md5


to give md5 the value on its standard input stream.



You could also use echo "$backup_dir" | md5 or md5 <<<"$backup_dir", but note that this adds a newline to the end of the value of $backup_dir which would alter the hash.



If md5 is the md5 utility commonly found on BSD and BSD-like systems (e.g. macOS), then you should use



md5 -q -s "$backup_dir"


The -s option takes a string as its argument, and -q causes md5 to only print out the hash of that string and nothing else.



Summary:



#!/bin/bash
backup_dir=$(date +%Y-%m-%d_%H-%M-%S)
hashed=$(md5 -q -s "$backup_dir")






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 18 at 15:37

























answered Feb 18 at 15:02









KusalanandaKusalananda

135k17255422




135k17255422







  • 1





    This works and looks clean. Thank you for the patient explanation!

    – Khom Nazid
    Feb 18 at 16:01












  • 1





    This works and looks clean. Thank you for the patient explanation!

    – Khom Nazid
    Feb 18 at 16:01







1




1





This works and looks clean. Thank you for the patient explanation!

– Khom Nazid
Feb 18 at 16:01





This works and looks clean. Thank you for the patient explanation!

– Khom Nazid
Feb 18 at 16:01

















draft saved

draft discarded
















































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%2f501377%2fin-a-bash-script-how-to-use-a-variable-inside-a-command-and-assign-to-a-new-var%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

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?