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 have a problem with feeding cut output to ls.
This doesn't work:

rep="S1_1000,0000-00-00c,0000-00-00d,0000-00-00e"  
ls tab_yeast/{`echo $rep | cut -f2- -d','`}*.tab.gz  
ls: cannot access tab_yeast/{0000-00-00c,0000-00-00d,0000-00-00e}*.tab.gz: No such file or directory

while ls over that string works:

ls tab_yeast/{0000-00-00c,0000-00-00d,0000-00-00e}*.tab.gz  

Any idea?

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

I do not believe you can nest commands with brace expansions. It appears to only have the ability to contain literals. You should be able to utilize eval to get what you want though.

Example

$ a=1
$ b=5
$ echo {$a..$b}
{1..5}

If you eval the echo however:

$ eval echo {$a..$b}
1 2 3 4 5

Your example

$ ls -l tab_yeast
total 0
-rw-rw-r--. 1 saml saml 0 Feb 21 05:40 0000-00-00c.1.tab.gz
-rw-rw-r--. 1 saml saml 0 Feb 21 05:40 0000-00-00d.1.tab.gz
-rw-rw-r--. 1 saml saml 0 Feb 21 05:40 0000-00-00e.1.tab.gz
-rw-rw-r--. 1 saml saml 0 Feb 21 05:40 S1_1000.1.tab.gz

show expanded

$ eval echo tab_yeast/{`echo $rep | cut -f2- -d','`}*.tab.gz  
tab_yeast/0000-00-00c.1.tab.gz tab_yeast/0000-00-00d.1.tab.gz tab_yeast/0000-00-00e.1.tab.gz

your modified command

$ ls -1 $(eval echo tab_yeast/{`echo $rep | cut -f2- -d','`}*.tab.gz)
tab_yeast/0000-00-00c.1.tab.gz
tab_yeast/0000-00-00d.1.tab.gz
tab_yeast/0000-00-00e.1.tab.gz
share|improve this answer
    
Thanks! I was trying eval, but without success. –  Leszek Feb 21 at 10:48
    
@Leszek - yes it can be a confusing tool to use. –  slm Feb 21 at 10:49
2  
+1. Due the order of shell expansions, command substitution and variable expansion cannot occur within a brace expression. –  glenn jackman Feb 21 at 11:41
add comment

In the first case, $rep is a string, echo $rep | cut -f2- -d',' seems to result in a string, and the brace extension does not take place :

$ ls tab_yeast/{`echo $rep | cut -f2- -d','`}*.tab.gz
ls: tab_yeast/0000-00-00c,0000-00-00d,0000-00-00e*.tab.gz: Aucun fichier ou répertoire de ce type

$ ls tab_yeast/{0000-00-00c,0000-00-00d,0000-00-00e}*.tab.gz
ls: tab_yeast/0000-00-00c*.tab.gz: Aucun fichier ou répertoire de ce type
ls: tab_yeast/0000-00-00d*.tab.gz: Aucun fichier ou répertoire de ce type
ls: tab_yeast/0000-00-00e*.tab.gz: Aucun fichier ou répertoire de ce type
share|improve this answer
add comment

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.