user3746406
user3746406

Reputation: 81

Change directories in shell scripts with string variables

I have a series of directories that are only different by a numerical tag.

arr=(0 1 2 3)
i=0
while [ $i -le ${arr}]
do
  dir="~Documents/seed" 
  dir+=${arr[i]}
  echo $dir #works
  cd dir #directory not found
  #do other things#
done

Is it possible to do this?

Upvotes: 1

Views: 55

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

This might be easier:

#!/bin/bash
for d in ~/Dcouments/seed*
do
   if [ -d "$d" ]; then
      echo $d
   fi
done

Note:

You have tarfiles in ~/Documents too (with names that also match the wildcard), so I have added an if statement that checks if it is a directory or a file and only reacts to directories.

Upvotes: 2

Related Questions