Bash Brace Expansion & Variables

Miati

I have a find command:

find Directory/{Alpha,Bravo,Charlie} arg1 arg2

I want to replace Alpha,Bravo,Charlie with $find_dir

find Directory/{$find_dir} arg1 arg2

however the latter expands to

find Directory/{Alpha,Bravo,Charlie} arg1 arg2

rather then

find Directory/Alpha Directory/Bravo Directory/Charlie arg1 arg2

Why? It's part of a fairly complex bash script that may have more or less directories, not all which are relevant (so globbing Upload/* would not work). So if I setup three now and add another, I'll have to manually add it in later. Plus I need it run from the root of directories to keep finds outputs perspective (./Upload/Dir/file as opposed to ./Dir/file).

But using a variable would permit me to change that as needed and keep it relevant to other parts of the script.

cuonglm

From bash documentation, Brace Expansion section:

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces. To avoid conflicts with parameter expansion, the string ‘${’ is not considered eligible for brace expansion.

Another note, in bash:

The order of expansions is: brace expansion, tilde expansion, parameter, variable, and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and filename expansion.

So in your case, bash saw brace expansion before variable expansion, it will do brace expansion first, produce result {Alpha,Bravo,Charlie}.

If you can control $find_dir variable content, you can use eval:

eval "find Directory/{$find_dir} arg1 arg2"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

bash shellcheck issue with variables in brace expansion

From Dev

Bash brace expansion with variables for pattern matching

From Dev

Brace expansion with numbers in bash

From Dev

BASH brace expansion algorithm

From Dev

Brace expansion with numbers in bash

From Dev

Bash Brace Expansion and cp

From Dev

bash - brace expansion not expanding?

From Dev

how to use variables with brace expansion

From Dev

Nested brace expansion mystery in Bash

From Dev

Bash Brace Expansion in Systemd ExecStart

From Dev

bash in-line brace expansion

From Dev

bash in-line brace expansion

From Dev

Nested brace expansion mystery in Bash

From Dev

`seq` and bash brace expansion failing

From Dev

How can I do brace expansion on variables?

From Dev

Why is Brace expansion with variables not working as expected?

From Dev

Change separator/delim in bash brace expansion

From Dev

Brace expansion with step increment not working bash

From Dev

Bash brace expansion after a path slash

From Dev

Bash brace expansion after a path slash

From Dev

Brace expansion with step increment not working bash

From Dev

Can I use a variable in a Bash brace expansion?

From Dev

In bash, is it possible to use an integer variable in a brace expansion

From Dev

Bash brace expansion to remove part of filename

From Dev

Change separator/delim in bash brace expansion

From Dev

Can I use bash brace expansion in for loop?

From Dev

Can't combine brace expansion with parameter expansion in bash

From Dev

How does bash differentiate between brace expansion and command grouping?

From Dev

How to output comma-separated strings using bash brace expansion

Related Related

HotTag

Archive