Questions tagged [brace-expansion]

A shell mechanism which is used to generate arbitrary strings.

Filter by
Sorted by
Tagged with
57
votes
7answers
17k views

When do you use brace expansion?

I understand what brace expansion is, but I don't know how best to use it. When do you use it? Please teach me some convenient and remarkable examples if you have your own tip.
54
votes
6answers
74k views

How to create a sequence with leading zeroes using brace expansion

When I use the following, I get a result as expected: $ echo {8..10} 8 9 10 How can I use this brace expansion in an easy way, to get the following output? $ echo {8..10} 08 09 10 I now that this ...
46
votes
4answers
19k views

How can I use $variable in a shell brace expansion of a sequence?

I want to use $var in a shell brace expansion with a range, in bash. Simply putting {$var1..$var2} doesn't work, so I went "lateral"... The following works, but it's a bit kludgey. # remove the ...
28
votes
2answers
3k views

What is the difference between `a[bc]d` (brackets) and `a{b,c}d` (braces)?

What is the difference between a[bc]d and a{b,c}d? Why do people use a{b,c}d when there is already a[bc]d?
25
votes
6answers
10k views

cat a very large number of files together in correct order

I have about 15,000 files that are named file_1.pdb, file_2.pdb, etc. I can cat about a few thousand of these in order by doing: cat file_{1..2000}.pdb >> file_all.pdb However, if I do this ...
22
votes
8answers
1k views

Apply brace expansion in “reverse order”

For example {a..c}{1..3} expands to a1 a2 a3 b1 b2 b3 c1 c2 c3. If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3, is there an analogous way to do that? What's the simplest way?
20
votes
5answers
24k views

bash: Use a variable to store stderr|stdout redirection

Is there any way to redirect stdout and stderr via variable like adding command options in script? For example I have a script: #!/bin/bash -x TEST=">/dev/null 2>&1" OPT='-p -v' mkdir $OPT ...
20
votes
3answers
3k views

Nested brace expansion mystery in Bash

This: $ echo {{a..c},{1..3}} produces this: a b c 1 2 3 Which is nice, but hard to explain given that $ echo {a..c},{1..3} gives a,1 a,2 a,3 b,1 b,2 b,3 c,1 c,2 c,3 Is this documented ...
17
votes
3answers
10k views

Using curly brackets (braces) to create folder structure with `mkdir -p`

As man mkdir states -p, --parents no error if existing, make parent directories as needed When I ran this command mkdir -p work/{F1,F2,F3}/{temp1,temp2} It creates a folder structure ...
14
votes
3answers
2k views

Why doesn't systemctl\ {restart,status}\ sshd\; work?

The output of the above command when passed through echo is: # echo systemctl\ {restart,status}\ sshd\; systemctl restart sshd; systemctl status sshd; Even if I paste the output to the terminal, the ...
13
votes
3answers
3k views

Possible to use brace (permutation) and array expansion simultaneously?

Bash's {} brace expansion syntax allows for creating easy permutations # echo {b,c,d}{a,e,i,o,u} ba be bi bo bu ca ce ci co cu da de di do du However it's not clear to me if/how it's possible to use ...
13
votes
4answers
1k views

When is {a,b,c} expanded in bash, when is it not?

A bash script that contains for i in {a,b}-{1,2}; do echo $i; done prints a-1 a-2 b-1 b-2 when executed. This is what I expected - as the {a,b} construct is expanded. However, when (another) ...
12
votes
3answers
2k views

Bash brace expansion after a path slash

I'm trying to copy a file to a different name into the same directory using brace expansion. I'm using bash 4.4.18. Here's what I did: cp ~/some/dir/{my-file-to-rename.bin, new-name-of-file.bin} ...
12
votes
3answers
2k views

Why does “cat {foo}” not output foo, but “cat {foo,bar}” does?

I was trying to concatenate text files in sub-folders and tried: cat ./{mainfolder1,mainfolder2,mainfolder3}/{subfolder1}/book.txt > out$var However this did not return anything. So, tried adding ...
11
votes
3answers
3k views

Bash expansion hexadecimal

I would like to know if there is a way of using bash expansion to view all possibilities of combination for a number of digits in hexadecimal. I can expand in binaries In base 2: echo {0..1}{0..1}{0....
11
votes
1answer
480 views

Why is echo matching some files twice?

$ touch file{1..12} $ echo *e{1..12} file1 file2 file3 file4 file5 file6 file7 file8 file9 file10 file11 file12 $ echo *{1..12} file1 file11 file12 file2 file3 file4 file5 file6 file7 file8 file9 ...
11
votes
2answers
14k views

bash combining wildcard expansion with brace expansion

I'm trying to expand a string involving a wildcard and a collection of extensions specified within braces. Nothing seems to work as the example below illustrates. the variable firstList expands ...
10
votes
3answers
5k views

Why is brace expansion not supported?

Do I have to do any configuration to start brace expansion? When I run mkdir {1..10}, it just creates a dir naming {1..10}.
10
votes
1answer
2k views

Why does brace expansion range use two dots instead of three (ellipsis)?

I finally understood/discovered why I never got to using bash's (and also zsh's) brace expansion range properly: the syntax is {1..10} instead of what I assumed to be an ellipsis, i.e. {1...10}. May ...
9
votes
4answers
308 views

Right-to-left shell brace expansion

In zsh (and other shells), if I include an argument like (for example): {a,b,c}{d,e,f} brace expansion turns it into: ad ae af bd be bf cd ce cf For my purposes, the argument order is important, ...
8
votes
4answers
2k views

How can I suppress the space between generated arguments during brace expansion?

I used the following command to create a list of URLs that I want to test: echo -e localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,....
8
votes
3answers
2k views

Why are bash brace expansions not working for commands?

Why is this working: mkdir /dir/test{1,2,3} and this not? {chown httpd,chmod 700} /dir/test1 -bash: {chown: command not found My Bash Version is: GNU bash, version 4.2.46(2)-release
8
votes
1answer
2k views

Append (alter) each array element via parameter expansion (i.e. without printf)?

Let the script below exemplify my quandary.. #!/bin/zsh STUFF=( moose-hoof ovary clydsedale ) echo ${MINE=$(printf "MY-%s " $STUFF)} echo ${MINE_EXP=${STUFF/^/MY-}} MY-moose-hoof MY-ovary MY-...
7
votes
3answers
1k views

Can I use variables inside {} expansion without `eval`?

Can I use variables inside {} expansion without appealing to eval? If so, how? This doesn't work: $ touch 1.foo 1.bar $ ls 1.{foo,bar} 1.bar 1.foo $ extensions=foo,bar $ ls 1.{$extensions} ls: ...
7
votes
1answer
406 views

Why in bash {{a,b}.{c,d}} expands to {a.c} {a.d} {b.c} {b.d}

In bash echo {{a,b}.{c,d}} expands to {a.c} {a.d} {b.c} {b.d} Though nothing is mentioned for brace expansion in bash manual for the type of input I have made My question is why the output is ...
7
votes
1answer
1k views

bash shellcheck issue with variables in brace expansion

I'm trying to create an array of file names, based on two variable and using brace expansion, like this: #!/bin/bash altdir=/usr arg=abc tries=({.,$altdir}/{$arg,$arg/main}.{tex,ltx,drv,dtx}) for i ...
7
votes
2answers
2k views

Any way to show each step during the command processing?

set -x just show a final result of the command. Any way to view each step more clear, like pics below.
6
votes
1answer
1k views

How do bash loops work precisely?

for i in {0..999999999}; do echo "hi"; done takes a long time to write it's output, as if all the echos are first executed and then sent to stdout. The effect is even more pronounced with ...
6
votes
1answer
378 views

Bash expansion asymmetry when opening and creating files

I have come upon the following example of asymmetry in a Bash regex that is confusing me. I would like to know what I am doing which is non-standard and causes this behaviour, or else what is the ...
6
votes
2answers
2k views

Loop in macOS not working

I need to execute the following shell script in my macOS terminal. The loop never executes more than its first iteration. function execute_function() { # Launch job number_of_jobs=$1 echo "...
6
votes
2answers
1k views

Bash brace expansion to remove part of filename

Is it possible to remove rather than adding substring to a filename using bash brace expansion? Considering the following scenario, one can add a suffix to a filename by using the below technique: ...
6
votes
1answer
363 views

Bash: Why does this Brace Expression work this way?

this is a bash 4 specific question. I am somewhat familiar with the bash Brace Expressions and I have read the manual but I do not understand how it actually works. I guess it has to do with the ...
5
votes
2answers
5k views

matching files using curly brace expansion in zsh

In bash I can use curly braces to pattern match several files, e.g. ls *.{dot,svg,err} If there's no file for a particular extension, I get a warning, but the remaining files will be listed. ls: ...
5
votes
3answers
759 views

Prepend and append a string to each element of $* in shell

I'm looking for the simplest solution that takes $* as input, and expands to each element prefixed and suffixed with a given string: $*=foo bar baz <solution(x,y)>=xfooy xbary xbazy I can do ...
5
votes
2answers
475 views

`seq` and bash brace expansion failing

IINM my system is failing when bashing for i in {0..10000000}; # Seven zeroes. do false; done # `bash` exited and its `tmux` pane/window was closed. or for i in $(seq 0 10000000); # Seven ...
5
votes
2answers
5k views

How could I make multiple symbolic links for multiple directories, conveniently

lrwxrwxrwx 1 deploy users 20 1월 23 18:15 v122 -> /home/files/video122 lrwxrwxrwx 1 deploy users 20 1월 23 18:15 v123 -> /home/files/video123 lrwxrwxrwx 1 deploy users 20 1월 23 18:...
5
votes
2answers
2k views

How to do multiple brace expansions without combinatorial explosion?

I often use brace expansion on the command line, because it's handy. Example {foo,bar} expands to: foo bar Multiple brace expansion also expand, e.g.: {foo,bar}Q{foo,bar} would expand to: fooQfoo ...
5
votes
2answers
399 views

Why arguments in braces do not expand in this case?

For example, I want to put some file names inside braces for expansion like this: $ ls blue_texture blue_waves green_light $ ls -m | tr -d ' \n' blue_texture,blue_waves,green_light $ echo foo/bar/{...
5
votes
2answers
657 views

Scrabble helper in bash

I'm trying to make a scrabble helper in bash, which when given a list of characters, finds all the words in the /usr/share/dict/words file. For example, when given the letters a,c,r,t The word cart ...
5
votes
2answers
864 views

How can I force bash to deallocate a brace expanded variable without name?

For benchmarking, I ran the command: for i in {1..100000000}; do echo "$i" line >> file done Bash expanded the braces and stored the list 1 2 3 4 5 6 ... 100000000 in memory. I thought ...
5
votes
2answers
198 views

Brace compression

I love brace expansion (e.g. rm file.{0..5}). I find it a lot easier to read than the expanded version. Is there a quick and easy way of doing the opposite? For example given the input www.example....
4
votes
4answers
602 views

How to use bash tricks to type out list of redundant strings which vary only in a couple characters?

This has probably been asked before, but I'm unsure how to word it. I want to type out a series of strings, which are basically string1, string2, string3; but without the redundant typing. So is there ...
4
votes
2answers
485 views

Bash script - variables in curly braces [duplicate]

The following bash script #!/bin/bash startNumber=$(( 1 )) endNumber=$(( $startNumber + 3 )) #for number in {$startNumber..$endNumber} for number in {1..4} do echo $number done exit 0 ...
4
votes
3answers
772 views

What is the difference between double-quoting and not double-quoting an array in Bash?

While tracking down an error in my shellscript, I found the following behavior in this code snippet: declare -a filelist readarray filelist < <(ls -A) readonly filelist for file in "${filelist[@...
4
votes
2answers
379 views

Brace expansion does not work for less than 2 arguments

I was expecting brace expansion to work for any number of arguments. However, for n=1 I get the following: $ find models/nsf-projects-{7}* models/nsf-projects-{7}.rdf For n>1 expansion occurs as ...
4
votes
2answers
1k views

brace expansion and * expansion

Assume in the working directory, there is only one djvu file. I would like to backup the file into a file with file name having extra .bk at the end. cp *.djvu{,.bk} copies the djvu file into a file ...
4
votes
3answers
1k views

Create arrays with brace expansion in loop

I would like to generate a number of arrays that differ only by year. In the loop, I create the arrays with brace expansion and a variable. I tried the following code without success: LIST={JF,JFE,...
4
votes
1answer
424 views

bash in-line brace expansion

Does bash/readline have a binding or a function that a key combination can be binded to in order to expand in-line braces? Similar to the way M-* key combination can be used for in-line globbing ...
4
votes
2answers
7k views

How does curly brace expansion work in the shell?

The command echo {1..3}-{1,2} prints 1-1 1-2 2-1 2-2 3-1 3-2. I understand the way those curly braces can be used. But what actually are they? Is it the job of sh / bash to parse/expand them and ...
4
votes
1answer
88 views

Deduplicating Bash Brace Expansions

Brace expansions can produce multiple instances of file names if there is an overlap in the matches. A simple example: mkdir testdir; cd testdir touch abcd for f in *{b,c}*; do something_to "$f"; ...