syntax error near unexpected token `

I am facing a problem when trying to echo all the child directory of the present directory.

Now my code is like this:

for d in */;
do echo "$d";
done

I tried with this as well:

for d in */;
do echo $d;
done

But every time I am getting the same error. This is:

'/test.sh: line 1: syntax error near unexpected token `
'/test.sh: line 1: `for d in */;

How to solve this? Definitely I am doing some mistake. But can't figure it out.

6

1 Answer

The command itself works fine:

$ for d in */; do echo "$d" ; done
subdir-A/
subdir-B/

However you have a hidden ' in front of the for command. Perhaps the file was copied from Windows. The easiest solution is to create a new test.sh file and copy the above command into it.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like