Kernel.Panic Kernel.Panic - 3 months ago 16
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 ../
times
n
, where
n
is whatever I pass to it, or
1
by default:

STEPS=${1:-1}
for i in $STEPS; do
cd ..
done


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

Answer

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 ..
done

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 ..
Comments