My bash script is written to go to each subfolder in the current directory:
for d in */; do target=${d%/} cd "$target" done
When I run my bash script, I am getting a cd error for a directory that exists:
++ for d in '*/' ++ target='Zelkova serrata' ++ cd 'Zelkova serrata' ./script7.sh: line 8: cd: Zelkova serrata: No such file or directory
Yet, in terminal command line I can do cd 'Zelkova serrata'
within the same directory as the script and all is fine. Is it possible the bash script has a different source directory than the one its located in?
Advertisement
Answer
You are looping through relative paths, try including the absolute path, for example:
#!/bin/bash pwd=$PWD for d in */; do target="$pwd/${d%/}" cd "$target" echo $PWD done