0

I have a Bash script:

How would I loop through a list of numbered variables and then make the variable into a loop.

Example:

//Variables
DIR="/home/"
LIMIT=8
1="cupcake/"
2="cake/"
3="icecream/"
4="donut/"
5="cocoa/"
6="whipcream/"
7="cookie/"
8="coffee/"

for i in {1..8} // The number of variables
do
  cd "$DIR${i}"; // cd /home/cupcake/
  make something;
done
1
  • 1
    N. B. Comments in shell-scripts starts with #, not with //. Commented Jan 4, 2014 at 15:07

4 Answers 4

1

Two ways will be reasonable.

Using positional parameters:

set "cupcake" "cake" "icecream" "donut" "cocoa" "whipcream" "cookie" "coffee"

for i; do
    cd "${DIR}/${i}" || continue
    # do something
done

It’s not in custom to store final slash of path in variables, so I use "${DIR}/${i}". Anyway, cd won’t fail at /home//cupcake.

Or using array:

A=( "cupcake" "cake" "icecream" "donut" "cocoa" "whipcream" "cookie" "coffee" )

for i in "${A[@]}"; do
    cd "${DIR}/${i}" || continue
    # do something
done

Please note, you should use "${A[@]}" to handle spaces properly, not ${A[*]}. || continue interrupts current iteration if cd fails.

Array obviously is more flexible: you can explicitly set a position as you tried to do in your question

A=(
    [1]="cupcake"
    [7]="cookie"
    [2]="cake"  
    [3]="icecream"
    [4]="donut"
    [6]="whipcream"
    [5]="cocoa"
    [8]="coffee"
)

edit any single element later

A[4]="doughnut"

delete any element

unset A[4]

and so on.

Sign up to request clarification or add additional context in comments.

Comments

1

I'll pretend that your numbered variables are parameters. That's what numbered variables are for.

for i in {1..8} // The number of variables
do
  eval cd "$DIR\${$i}"; // cd /home/cupcake/
  make something;
done

However, if this is indeed parameters, you can safely use $1 and keep shifting until you've referenced them all.

for i in {1..8} // The number of variables
do
  cd "$DIR$1"; // cd /home/cupcake/
  make something;
  shift
done

Instead of using for {1..8} you can check for the number of parameters passed $#.

while [ $# -gt 0 ] // The number of variables
do
  cd "$DIR$1"; // cd /home/cupcake/
  make something;
  shift
done

1 Comment

There is more safer and clear way than eval to reference variable indirect: ${!i}. cd "${DIR}${!i}"
0

You can't. Numbered variables are reserved for script/function arguments. Rename them.

1 Comment

You are right that they are reserved. But you can set and reset them at will. Try set one two three four then check the values of $1 to $4.
0

I suspect you want an array which also allows a for loop, although it would look more like

for subdir in ${dirarray[*]}
do
    cd "$DIR$subdir";
    make something;
done

1 Comment

for subdir in ${dirarray[*]} it fails to handle spaces. Use "${dirarray[@]}".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.