Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
Removing '-' on linux md5sum

When I try to encode the password variable into md5sum, it is giving me a string with the md5sum encoding and a dash at the end. And when I try to put a dash after it, it doesn't work. So my questions is: How can I remove the dash on the md5sum output of the linux terminal? Here is my code:

#!/bin/bash

read -p 'Password: ' passwordInput
Password=$(echo -n "$passwordInput" | md5sum)
if [ $Password = "0cbc6611f5540bd0809a388dc95a615b" ]; then
    clear
    echo "Password is good"
else
    clear
    echo "Password is bad"
fi
exit 0;
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

#!/bin/bash

read Test
Password=$(echo -n "$Test" | md5sum | cut -d' ' -f1)
echo ""
if [ $Password = "d41d8cd98f00b204e9800998ecf8427e" ]; then
      clear
      echo "WhOop WHOop"
else
      echo ":("
fi
over 4 years ago · Santiago Trujillo Report

0

You have three problems.

First, your question is not very clear. Please update it to state clearly just what you're asking (how you want your program to behave, and how it fails to do so). In this case it's not too hard to figure out from your code, but that's not always true. You write

I don't get the "-" out of the linux md5sum.

but in fact the problem is that you are getting the "-" (and probably more as well).

(UPDATE: Better now, thanks.)

Second, the output of the md5sum includes some extra information after the displayed checksum, and you need to remove it. (It would be nice if md5sum had an option to print just the checksum, but it doesn't.) Fortunately, it's easy to strip the extra information; it's just a space character and everything following it.

Third, because you're using single quotes:

Password="$(echo -n '$Test' | md5sum)"

the string you're passing to md5sum is literally '$Test', not the value of your variable.

Fixing the second and third problems:

#!/bin/bash

read Test
Password="$(echo -n "$Test" | md5sum | sed 's/ .*$//')"
echo ""
if [ "$Password" = "d41d8cd98f00b204e9800998ecf8427e" ]; then
    clear
    echo "WhOop WHOop"
else
    echo ":("
fi

I'd normally criticize your use of the clear command, but in this case you're erasing a password that the user entered. But you're not clearing the screen if the user enters an incorrect password. Bash's read command has an option to suppress echoing of input, and this is the perfect use for it. Here's how I might do it:

#!/bin/bash

read -p "Password: " -s Test
Password="$(echo -n "$Test" | md5sum | sed 's/ .*$//')"
echo ""
if [ "$Password" = "d41d8cd98f00b204e9800998ecf8427e" ]; then
    echo "WhOop WHOop"
else
    echo ":("
fi

(Incidentally, the empty string is a lousy password, as is anything whose md5sum can be found with a quick Google search -- like "Test".)

over 4 years ago · Santiago Trujillo Report

0

The "-" in the md5sum indicates, that you are using standard input for md5sum. If you would be using md5sum for a file, a filename would be displayed in that place.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!