Kernel.Panic Kernel.Panic - 1 year ago 79
Bash Question

This simple bash script doesn't work. What am I doing wrong?

I'm trying to create the script that will simply do

cd ../
, where
is whatever I pass to it, or
by default:

for i in $STEPS; do
cd ..

It doesn't give me any errors and it does nothing..

Answer Source

In bash, you generally don't want to generate a sequence of numbers to iterate over. Use the C-style for loop:

for ((i=1; i<=$STEPS; i++)); do
  cd ..

If this is in a file, you need to source it ( . ./cd-up) rather than executing it (sh ./cd-up or ./cd-up, etc).

If you are, in fact, using zsh, you can simply use the repeat construct:

repeat $STEPS do cd ..; done

or its shorter form for simple commands

repeat $STEPS cd ..
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download