Possible, yes, but you really should avoid such programming. Instead, you could use arrays for example. Here's a small bash script checking all files in the current directory, counting the number of files with the same extensions:
Code:
#!/bin/bash
# Declare $extensions and $counts to be arrays. Not really necessary, but it
# shows that you intend these to be arrays.
declare -a extensions
declare -a counts
for file in *.*; do
# Set $ext to $file with everything up to the last '.' removed.
ext="${file##*.}"
# Check if $extensions has this extension already and increment the count
# for that extension if that is the case
for ((i=0; i < ${#extensions[*]}; i++)); do
if [ "$ext" = "${extensions[i]}" ]; then
((counts[i]++))
break
fi
done
# If $i equals the size of the array, there wasn't an entry for that
# extension already, so we add a new element to the arrays.
if [ $i -eq ${#extensions[*]} ]; then
extensions[i]="$ext"
counts[i]=1
fi
done
# Print the results
for ((i=0; i < ${#extensions[*]}; i++)); do
echo "There are ${counts[i]} files with '${extensions[i]}' extension"
done
To do what you originally wanted, you'd need to use eval
Code:
$ var=txt
$ eval ${var}_count=2
$ echo $txt_count
2
Bookmarks