0

How can I veify a folder name in a bash script.

I am in the folder named "test". Now I want to check the is really "text".

I have tried the following:

cd tmp
mkdir testfolder
cd testfolder
if [["${PWD##*/}"]] == "testfolder"
then echo "ok"
fi
done

But always get the error, that testfolder not found, it tries to run if as a command.

Thanks

1
  • Please always include the exact error messages you get in the question Commented Oct 20, 2016 at 14:02

2 Answers 2

1

The actual check condition should have been

if [ "${PWD##*/}" == "testfolder" ];

Or you can use the test operator [[]] with the return code of the comparison performed, something like:-

 [[ "${PWD##*/}" == "testfolder" ]] && echo "Match"
Sign up to request clarification or add additional context in comments.

2 Comments

you have to wait some minutes, so not always :-)
nit: If you use [, you should use =. That is if [ "${PWD##*/}" = testfolder ]; Most modern shell's builtin implementation of test will recognize ==, but /bin/[ on a recent Darwin (15.6.0) throws an error.
1

You need to include the whole comparison into [[ ... ]]:

if [[ ${PWD##*/} == testfolder ]]

Also note that spaces around [[ aren't optional. Double quotes aren't needed in [[ ... ]], but they are needed if you switch to single [ ... ].

1 Comment

thank you I think I have tried this 10 times before but now it is working :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.