DaWolfman
DaWolfman

Reputation: 79

How do you reference a variable within a string in UNIX?

I am writing a new script that creates aliases based on the results of an ls query. The script itself is simple enough, however what is hanging me up is the alias call. I cannot figure out how to reference the variable that I've generated in another string when I call the alias command:

for f in `ls -1 -O dir /dev/fs/`
do
  fl=`echo $f | tr '[:upper:]' '[:lower:]'`
  alias "$fl"_drive='cd /dev/fs/$f'
done

It's that reference to the $f variable in the alias call that's throwing me off. Can anyone shed some light on the proper syntax for this? Thanks in advance.

Upvotes: 1

Views: 1079

Answers (2)

BMitch
BMitch

Reputation: 265060

Try:

alias "${fl}_drive"="cd /dev/fs/$f"

Upvotes: 2

Benjamin Bannier
Benjamin Bannier

Reputation: 58774

I guess you properly want $f to point to the file you currently loop over.

The shell doesn't expand anything inside single quotes ('), so your alias contains a literal $f. Use double quotes instead.

alias "$fl"_drive="cd /dev/fs/$f"

Upvotes: 3

Related Questions