oxidworks oxidworks - 1 year ago 113
Bash Question

Verify folder name in a bash script

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"

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


Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download