In this specific case, note that bash has a variable called PWD that contains the current directory: $PWD is equivalent to `pwd`. (So do other shells, this is a standard feature.) So you can write your script like this:
#!/bin/bash
until [ "$PWD" = "/" ]; do
echo "$PWD"
ls && cd .. && ls
done
Note the use of double quotes around the variable references. They are necessary if the variable (here, the current directory) contains whitespace or wildcards (\[?*), because the shell splits the result of variable expansions into words and performs globbing on these words. Always double-quote variable expansions "$foo" and command substitutions "$(foo)" (unless you specifically know you have not to).
In the general case, as other answers have mentioned already:
- You can't use whitespace around the equal sign in an assignment:
var=value, not var = value
- The
$ means “take the value of this variable”, so you don't use it when assigning: var=value, not $var=value.
i=0oustide of the loop. then inside,i=$i + 1. And then also inside the loop, add something likeif [ $i > 25 ] then; break; endif;I'm not sure of the loop breaking in syntax in shell scripts, but it should be something like that.[ $i > 25 ], is exactly the same as[ $i ] >25; that is to say,25is treated as a filename. You need to use-gtfor numeric comparisons, or quote'>'for string comparisons. And adding one toiisi=$(( i + 1)); the syntax you have now tries to run the command+with the argument1andiset in its environment. And the keyword for ending anifin shell isfi.