Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I wanted to declare an environment variable that stocks all the extensions of video files so I can use it when using the shell.

I tried several things but never got it to work: If in my .bash_profile I put:

export VIDEOS={mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}

it only takes the last element:

[nbarraille@nbarraille ~]echo $VIDEOS
mpeg

If in my .bash_profile I put:

export VIDEOS="{mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}"

or

export VIDEOS='{mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}'

Then, when I display it it looks OK, but it doesn't work when I use it in a ls for example:

[nbarraille@nbarraille ~] echo $VIDEOS
{mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}
[nbarraille@nbarraille ~] ll *.$VIDEOS
ls: cannot access *.{mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}: No such file or directory

And when I run the exact same command without using the variable, it works:

[nbarraille@nbarraille ~] echo *.{mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}
example.avi
example2.mpg

Also, when I reboot, it looks like my .bash_profile is not loading, and the $VIDEOS variable is empty. I have to do a source ~/.bash_profile in order to get it to work (and I have to redo so every time I open a new terminal.

Any idea?

Thanks!

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Your command is being expanded to this:

export VIDEOS=mp4 VIDEOS=wmv VIDEOS=avi VIDEOS=flv VIDEOS=mkv VIDEOS=m4u VIDEOS=mpg VIDEOS=mpeg

Run this to see what's happening:

echo export VIDEOS={mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}

(it's called brace expansion)


The second problem is that bash does brace expansion before parameter expansion, not after it, so anything that looks like your solution will be messy.

You would have to do something like this:

eval echo \*.$VIDEOS

which will get annoying to type every time.

How about something like this:

videos() {
    find . -mindepth 1 -maxdepth 1 -type f \
        \( -name "*.mp4" -o -name "*.wmv" -o -name "*.avi" -o \
           -name "*.flv" -o -name "*.mkv" -o -name "*.m4u" -o \
           -name "*.mpg" -o -name "*.mpeg" \)
}

Then instead of doing:

ls *.$VIDEOS

just do this:

videos

or if you need to pass it to a command:

ls $(videos)

This part working:

echo *.{mp4,wmv,avi,flv,mkv,m4u,mpg,mpeg}

could be the clue to .bash_profile not working. For example, it might mean you are using zsh.

Please tell us what this does:

echo $0

so we can figure out which file you have to put it in.

share|improve this answer
    
echo $0 => /bin/bash –  nbarraille Jan 25 '11 at 4:58
    
Well, then it either needs to go in .bashrc or .bash_profile, depending on your setup. Please try both. –  Mikel Jan 25 '11 at 5:17
    
Ok, thanks for your post it was useful. I will try to put it in .bashrc and I'll let you know. –  nbarraille Jan 25 '11 at 12:33

You could probably use arrays for this. Your syntax will have to be a bit different:

export VIDEOS=(.mp4 .wmv .avi .flv .mkv .m4u .mpg .mpeg)

and then do

ls ${VIDEOS[*]/./*.}

If you only want to list videos with names starting with foo, you would do

ls ${VIDEOS[*]/./foo*.}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.