nesting brace expansion and command substitution
Clash 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?
bash ls seq
add a comment |Â
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?
bash ls seq
"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 (yourls 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
Thecannot access
error message fromls
always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version ofls
, though. Try something likels foo "foo "
â ilkkachu
21 mins ago
add a comment |Â
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?
bash ls seq
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
bash ls seq
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 (yourls 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
Thecannot access
error message fromls
always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version ofls
, though. Try something likels foo "foo "
â ilkkachu
21 mins ago
add a comment |Â
"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 (yourls 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
Thecannot access
error message fromls
always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version ofls
, though. Try something likels 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
add a comment |Â
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.
1
Thanks. I thought about this, but for an unknown reason didn't test it.
â ka3ak
2 hours ago
add a comment |Â
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.
1
Thanks. I thought about this, but for an unknown reason didn't test it.
â ka3ak
2 hours ago
add a comment |Â
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.
1
Thanks. I thought about this, but for an unknown reason didn't test it.
â ka3ak
2 hours ago
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
"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 fromls
always shows the name in question surrounded with quotes, that has nothing to do with your problem. It might depend on the version ofls
, though. Try something likels foo "foo "
â ilkkachu
21 mins ago