nesting brace expansion and command substitution

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I want to create a simple bash-script that checks whether a directory contains all the files whose names contain numbers from 1 to N.



# Creating some files for testing
$ cd /tmp/
$ mkdir test
$ touch test/a01x.dat
$ touch test/b02y.dat

# Display dir contents
$ ls test/*01,02*
test/a01x.dat test/b02y.dat


But using seq command to generate numbers results in the following:



$ ls test/*$(seq -s , -f "%02g" 1 2)*
ls: cannot access 'test/*01,02*': No such file or directory


I understand that running the command by surrounding the path with single quotation marks must lead to the error because the wildcards don't expand



$ ls 'test/*01,02*'


But I didn't use them. What is the problem?










share|improve this question























  • "check whether a directory contains all the files" - do you want to do something if a file doesn't exist with that number in it? Or do you just want to show files that contain one of those numbers as part of the file name (your ls test/*01,02* seems to indicate so...) ? Also, what format will the numbers be in? If N=100 will you use 001 or 1 or ?? to start?
    – ivanivan
    3 hours ago











  • @ivanivan I've renamed the question
    – ka3ak
    3 hours ago










  • The cannot access error message from ls always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version of ls, though. Try something like ls foo "foo "
    – ilkkachu
    21 mins ago














up vote
1
down vote

favorite












I want to create a simple bash-script that checks whether a directory contains all the files whose names contain numbers from 1 to N.



# Creating some files for testing
$ cd /tmp/
$ mkdir test
$ touch test/a01x.dat
$ touch test/b02y.dat

# Display dir contents
$ ls test/*01,02*
test/a01x.dat test/b02y.dat


But using seq command to generate numbers results in the following:



$ ls test/*$(seq -s , -f "%02g" 1 2)*
ls: cannot access 'test/*01,02*': No such file or directory


I understand that running the command by surrounding the path with single quotation marks must lead to the error because the wildcards don't expand



$ ls 'test/*01,02*'


But I didn't use them. What is the problem?










share|improve this question























  • "check whether a directory contains all the files" - do you want to do something if a file doesn't exist with that number in it? Or do you just want to show files that contain one of those numbers as part of the file name (your ls test/*01,02* seems to indicate so...) ? Also, what format will the numbers be in? If N=100 will you use 001 or 1 or ?? to start?
    – ivanivan
    3 hours ago











  • @ivanivan I've renamed the question
    – ka3ak
    3 hours ago










  • The cannot access error message from ls always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version of ls, though. Try something like ls foo "foo "
    – ilkkachu
    21 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to create a simple bash-script that checks whether a directory contains all the files whose names contain numbers from 1 to N.



# Creating some files for testing
$ cd /tmp/
$ mkdir test
$ touch test/a01x.dat
$ touch test/b02y.dat

# Display dir contents
$ ls test/*01,02*
test/a01x.dat test/b02y.dat


But using seq command to generate numbers results in the following:



$ ls test/*$(seq -s , -f "%02g" 1 2)*
ls: cannot access 'test/*01,02*': No such file or directory


I understand that running the command by surrounding the path with single quotation marks must lead to the error because the wildcards don't expand



$ ls 'test/*01,02*'


But I didn't use them. What is the problem?










share|improve this question















I want to create a simple bash-script that checks whether a directory contains all the files whose names contain numbers from 1 to N.



# Creating some files for testing
$ cd /tmp/
$ mkdir test
$ touch test/a01x.dat
$ touch test/b02y.dat

# Display dir contents
$ ls test/*01,02*
test/a01x.dat test/b02y.dat


But using seq command to generate numbers results in the following:



$ ls test/*$(seq -s , -f "%02g" 1 2)*
ls: cannot access 'test/*01,02*': No such file or directory


I understand that running the command by surrounding the path with single quotation marks must lead to the error because the wildcards don't expand



$ ls 'test/*01,02*'


But I didn't use them. What is the problem?







bash ls seq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 24 mins ago









ilkkachu

52.9k680145




52.9k680145










asked 3 hours ago









ka3ak

544516




544516











  • "check whether a directory contains all the files" - do you want to do something if a file doesn't exist with that number in it? Or do you just want to show files that contain one of those numbers as part of the file name (your ls test/*01,02* seems to indicate so...) ? Also, what format will the numbers be in? If N=100 will you use 001 or 1 or ?? to start?
    – ivanivan
    3 hours ago











  • @ivanivan I've renamed the question
    – ka3ak
    3 hours ago










  • The cannot access error message from ls always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version of ls, though. Try something like ls foo "foo "
    – ilkkachu
    21 mins ago
















  • "check whether a directory contains all the files" - do you want to do something if a file doesn't exist with that number in it? Or do you just want to show files that contain one of those numbers as part of the file name (your ls test/*01,02* seems to indicate so...) ? Also, what format will the numbers be in? If N=100 will you use 001 or 1 or ?? to start?
    – ivanivan
    3 hours ago











  • @ivanivan I've renamed the question
    – ka3ak
    3 hours ago










  • The cannot access error message from ls always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version of ls, though. Try something like ls foo "foo "
    – ilkkachu
    21 mins ago















"check whether a directory contains all the files" - do you want to do something if a file doesn't exist with that number in it? Or do you just want to show files that contain one of those numbers as part of the file name (your ls test/*01,02* seems to indicate so...) ? Also, what format will the numbers be in? If N=100 will you use 001 or 1 or ?? to start?
– ivanivan
3 hours ago





"check whether a directory contains all the files" - do you want to do something if a file doesn't exist with that number in it? Or do you just want to show files that contain one of those numbers as part of the file name (your ls test/*01,02* seems to indicate so...) ? Also, what format will the numbers be in? If N=100 will you use 001 or 1 or ?? to start?
– ivanivan
3 hours ago













@ivanivan I've renamed the question
– ka3ak
3 hours ago




@ivanivan I've renamed the question
– ka3ak
3 hours ago












The cannot access error message from ls always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version of ls, though. Try something like ls foo "foo "
– ilkkachu
21 mins ago




The cannot access error message from ls always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version of ls, though. Try something like ls foo "foo "
– ilkkachu
21 mins ago










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










The problem is that the shell will do glob expansion (which includes ...,...) before it does command substitution (the $(...) part.) So after your seq is expanded, the shell will not re-evaluate the 01,02 and will leave it as a literal.



You need to add an eval to have it re-evaluate the expression after the command substitution is performed:



$ eval "ls test/*$(seq -s , -f "%02g" 1 2)*"


In this case, the command substitution will be performed first, resulting in a string ls test/*01,02* and the eval will ask the shell to interpret this as a command, which will then perform the glob expansion, resulting in the list of files you're expecting.






share|improve this answer
















  • 1




    Thanks. I thought about this, but for an unknown reason didn't test it.
    – ka3ak
    2 hours ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476734%2fnesting-brace-expansion-and-command-substitution%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










The problem is that the shell will do glob expansion (which includes ...,...) before it does command substitution (the $(...) part.) So after your seq is expanded, the shell will not re-evaluate the 01,02 and will leave it as a literal.



You need to add an eval to have it re-evaluate the expression after the command substitution is performed:



$ eval "ls test/*$(seq -s , -f "%02g" 1 2)*"


In this case, the command substitution will be performed first, resulting in a string ls test/*01,02* and the eval will ask the shell to interpret this as a command, which will then perform the glob expansion, resulting in the list of files you're expecting.






share|improve this answer
















  • 1




    Thanks. I thought about this, but for an unknown reason didn't test it.
    – ka3ak
    2 hours ago














up vote
4
down vote



accepted










The problem is that the shell will do glob expansion (which includes ...,...) before it does command substitution (the $(...) part.) So after your seq is expanded, the shell will not re-evaluate the 01,02 and will leave it as a literal.



You need to add an eval to have it re-evaluate the expression after the command substitution is performed:



$ eval "ls test/*$(seq -s , -f "%02g" 1 2)*"


In this case, the command substitution will be performed first, resulting in a string ls test/*01,02* and the eval will ask the shell to interpret this as a command, which will then perform the glob expansion, resulting in the list of files you're expecting.






share|improve this answer
















  • 1




    Thanks. I thought about this, but for an unknown reason didn't test it.
    – ka3ak
    2 hours ago












up vote
4
down vote



accepted







up vote
4
down vote



accepted






The problem is that the shell will do glob expansion (which includes ...,...) before it does command substitution (the $(...) part.) So after your seq is expanded, the shell will not re-evaluate the 01,02 and will leave it as a literal.



You need to add an eval to have it re-evaluate the expression after the command substitution is performed:



$ eval "ls test/*$(seq -s , -f "%02g" 1 2)*"


In this case, the command substitution will be performed first, resulting in a string ls test/*01,02* and the eval will ask the shell to interpret this as a command, which will then perform the glob expansion, resulting in the list of files you're expecting.






share|improve this answer












The problem is that the shell will do glob expansion (which includes ...,...) before it does command substitution (the $(...) part.) So after your seq is expanded, the shell will not re-evaluate the 01,02 and will leave it as a literal.



You need to add an eval to have it re-evaluate the expression after the command substitution is performed:



$ eval "ls test/*$(seq -s , -f "%02g" 1 2)*"


In this case, the command substitution will be performed first, resulting in a string ls test/*01,02* and the eval will ask the shell to interpret this as a command, which will then perform the glob expansion, resulting in the list of files you're expecting.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Filipe Brandenburger

4,530623




4,530623







  • 1




    Thanks. I thought about this, but for an unknown reason didn't test it.
    – ka3ak
    2 hours ago












  • 1




    Thanks. I thought about this, but for an unknown reason didn't test it.
    – ka3ak
    2 hours ago







1




1




Thanks. I thought about this, but for an unknown reason didn't test it.
– ka3ak
2 hours ago




Thanks. I thought about this, but for an unknown reason didn't test it.
– ka3ak
2 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476734%2fnesting-brace-expansion-and-command-substitution%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke