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 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"
2
  • 1
    you have to wait some minutes, so not always :-)
    – oxidworks
    Oct 20, 2016 at 14:33
  • 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. Oct 20, 2016 at 15:24
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
  • thank you I think I have tried this 10 times before but now it is working :-)
    – oxidworks
    Oct 20, 2016 at 14:10

Your Answer

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

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