Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to make a script that deletes a directory with rmdir after confirming with a password using read to set the variable.

So far I have this:

#!/bin/bash -x
echo "Password:"
read -t 30 S1
if [ $S1=$S2 ]; then
    rmdir /home/william/test
    echo "fail"
sleep 10

So, I have the -x to try to debug it but every time the script either fails to echo (if I put the password in wrong) or it wont remove the directory needed.

If someone has a modifiable script that I could use or if you could point out the problems with the current script that would be great.

share|improve this question

The right way to compare those two strings (S1 and S2) using if is:

if [ "$S1" = "$S2" ]

Do not be stingy in use spaces in this case.


share|improve this answer

In bash scripting you need to compare two variable with below method.

if [ "var1" != "var2" ]; then
  Do something

Spaces are important

share|improve this answer

You may also use GNU test, for example:

test s1 = s2 && echo Equal || echo Not equal

In your context, it is:

test "$S1" = "$S2" && rmdir -v /home/william/test
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.