Sample Header Ad - 728x90

Output of loop variable shows different value than expected

-1 votes
1 answer
73 views
Imagine I have two folders in my current working directory: back/ and front/. I am trying to enter inside each of them and do some stuff. From the simple script below:
-shell
for dir in */ ; do
    echo "$(dir)"
done
I was expecting an output like:
back
front
However, what I am getting is:
back  front
back  front
I don't understand. The consequence is that, if I try to cd into the folder:
-shell
for dir in */ ; do
    echo "$(dir)"
    cd $(dir)
    echo "$(pwd)"
    cd ..
done
I get:
back  front
/path/to/back
back  front
/path/to/back
I.e. I never enter front/. Could someone please help me understand what I am doing wrong?
Asked by Rigel F. do C. (1 rep)
Apr 22, 2024, 03:06 PM
Last activity: Apr 24, 2024, 12:50 PM