Skip to main content
added 475 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
cd -P .
for dir in ./*/
do cd -P "$dir" &&||continue
   printf %s\\n "$PWD" >&2
   command && cd "$OLDPWD" || 
! break; done || ! cd - >&2

The above command doesn't need to do any subshells - it just tracks its progress in the current shell by alternating $OLDPWD and $PWD. When you cd - the shell exchanges the value of these two variables, basically, as it changes directories. It also prints the name for each directory as it works there to stderr.

I just had a second look at it and decided I could do a better job with error handling. It will skip a dir into which it cannot cd - and cd will print a message about why to stderr - and it will break w/ a non-zero exit code if your command does not execute successfully or if running command somehow affects its ability to return to your original directory - $OLDPWD. In that case it also does a cd - last - and writes the resulting current working directory name to stderr.

cd -P .
for dir in ./*/
do cd -P "$dir" &&
   printf %s\\n "$PWD" >&2
   command && cd "$OLDPWD"
done

The above command doesn't need to do any subshells - it just tracks its progress in the current shell by alternating $OLDPWD and $PWD. When you cd - the shell exchanges the value of these two variables, basically, as it changes directories. It also prints the name for each directory as it works there to stderr.

cd -P .
for dir in ./*/
do cd -P "$dir" ||continue
   printf %s\\n "$PWD" >&2
   command && cd "$OLDPWD" || 
! break; done || ! cd - >&2

The above command doesn't need to do any subshells - it just tracks its progress in the current shell by alternating $OLDPWD and $PWD. When you cd - the shell exchanges the value of these two variables, basically, as it changes directories. It also prints the name for each directory as it works there to stderr.

I just had a second look at it and decided I could do a better job with error handling. It will skip a dir into which it cannot cd - and cd will print a message about why to stderr - and it will break w/ a non-zero exit code if your command does not execute successfully or if running command somehow affects its ability to return to your original directory - $OLDPWD. In that case it also does a cd - last - and writes the resulting current working directory name to stderr.

Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242

cd -P .
for dir in ./*/
do cd -P "$dir" &&
   printf %s\\n "$PWD" >&2
   command && cd "$OLDPWD"
done

The above command doesn't need to do any subshells - it just tracks its progress in the current shell by alternating $OLDPWD and $PWD. When you cd - the shell exchanges the value of these two variables, basically, as it changes directories. It also prints the name for each directory as it works there to stderr.