Questions tagged [brace-expansion]

A shell mechanism which is used to generate arbitrary strings.

Filter by
Sorted by
Tagged with
0 votes
0 answers
10 views

How to use a variable in a if then timer loop [duplicate]

I want to have a timer count down to zero from a value determined by variable setTime in my bash script. However, when I define setTime and type it into my if then loop in the timer, the script doesn'...
user avatar
8 votes
2 answers
763 views

Avoiding non-zero exit code when running `ls` using multiple patterns

Say I have two possible paths I want to list directories and files under on a Linux machine: /some/path1/ /some/path2/ If I do the following in tcsh, I get 0 exit code, if at least one of path1 or ...
user avatar
3 votes
3 answers
113 views

Store expanded array in a variable using a brace expansion

I want to store node1 node2 node3 in a variable so that it may be used as an input to other scripts. However, I want to do so by using brace expansion given by bash like so: node{1..3} I tried to have ...
user avatar
0 votes
1 answer
48 views

Is it possible to get the entire result of bash brace expansion between double quotes?

I would like to use Bash brace expansion and get the final result quoted between double quotes: I managed to get that result only by using seq as follows: echo '"'$(seq 1 5)'"'; output (...
user avatar
2 votes
2 answers
66 views

brace expansion together with non permuting lists {txtfile,index}{1..3}.{txt,html} I want `{txtfile,index}` to correspond with `{txt,html}`

{txtfile,index}{1..3}.{txt,html} I want {txtfile,index} to correspond with {txt,html} to produce: txtfile1.txt txtfile2.txt txtfile3.txt index1.html index2.html index3.html but this code will generate ...
user avatar
2 votes
2 answers
48 views

bash brace expansion: is it possible to associate another list(s) with the initial list?

Example: $ eval echo "{x,y,z}\ --opt\; " x --opt; y --opt; z --opt; Assume that the 2nd list is {1,2,3} and its length is equal to the length of the 1st (initial) list. Questions: How to ...
user avatar
  • 355
2 votes
2 answers
2k views

rsync: How to exclude multiple file types?

This is with bash on a Mac running Catalina: This works: rsync -Pa --rsh="ssh -p 19991" --exclude '*.jpg' --exclude '*.mp4' pi@localhost:/home/pi/webcam /Volumes/Media/Webcam\ Backups/...
user avatar
  • 75
0 votes
2 answers
54 views

Print expression with [ ]

I have an input in form A[BCDE]GT. I would like my output to be: ABGT ACGT ADGT AEGT I have tried to use echo, printf, trying to find something that might work. Does anyone know some command that ...
user avatar
  • 1
0 votes
1 answer
100 views

How to retrieve items from an array of arrays?

Hello StackExchange pros! I am working on a zsh project for macOS. I used typeset to create three associative arrays to hold values, and a fourth array to reference the individual arrays. Is it ...
user avatar
  • 3
1 vote
1 answer
197 views

What does {A,B} mean in shell?

I am reading this file, a manifest for a flatpak application. There is the following command: ln -s /app/{extra,bin}/masterpdfeditor5 What {extra,bin} mean?
user avatar
0 votes
1 answer
62 views

How does the {,string} syntax work?

I have some hard time understanding the {,string} syntax. I mainly use it to rename a files like mv test{,.original} But I don't really understand it: echo asdf{,.fdsa} Output: asdf asdf.fdsa But ...
user avatar
6 votes
1 answer
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 ...
user avatar
  • 2,643
0 votes
2 answers
51 views

Bash brace extension for 2 identical ranges

I wanted to download a bunch of numbered text files by: curl https://www.grc.com/sn/sn-{472..807}.txt > sn-{472..807}.txt but that gave me: bash: sn-{472..807}.txt: ambiguous redirect so I ended ...
user avatar
  • 5,562
6 votes
1 answer
393 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 ...
user avatar
  • 415
3 votes
2 answers
561 views

Unwanted space in brace expansion

I have the following brace expansion (bash shell): echo -e {0..4..2}" "{0..2..2}"\n" I expected this to produce 0 0 0 2 2 0 2 2 4 0 4 2 but every line of the output except the ...
user avatar
0 votes
1 answer
60 views

Is my simpler shell snippet the same as original?

I am in a process of doing a yearly review of my POSIX shell code. I may have found one weird line, which I did not know better before: original [ $# -eq 3 ] && exit_code=$3 || exit_code=1 ...
user avatar
4 votes
2 answers
929 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 ...
user avatar
  • 143
0 votes
1 answer
30 views

Printing Jan, Feb, March and April of 2020 with Cal [closed]

I learnt brace expansion today and tried the following command $ cal {1...4} 2020 I expected the output to display the calendar of Jan, Feb, March and April for the year 2020. But I got the following ...
user avatar
0 votes
2 answers
139 views

Directory creation using brace expansion

mkdir Public/1 2 3 ----- This line creates folder 1 in the Public folder and creates folders named 2 and 3 in my pwd. mkdir Public/{1..3} ---- This line creates folders 1,2,3 in Public only as ...
user avatar
0 votes
2 answers
127 views

Pathname expansion inside brace expansion

The following code is working: mkdir /home/karan/{Pictures,Public}/yo The above line creates a yo named directory in the directories named Public and Pictures. However when I use the below code I get ...
user avatar
2 votes
1 answer
76 views

zsh get numbered log files in backwards order

I have rolling log files that are numbered such that most recent queries are in x.log until that file hits a threshold size, then it's renamed to x.log.1 and a new x.log starts. I am writing a basic ...
user avatar
  • 559
0 votes
4 answers
283 views

Bash ls with double substitution

I am writing a bash script that reads a directory from the command line arguments and performs an ls on it. If I specify a simple directory, then all is OK. But I want do something like /home/{x,y}, ...
user avatar
5 votes
3 answers
2k 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 ...
user avatar
  • 153
3 votes
2 answers
203 views

Brace expansion with elements of an array

In Bash we can already do this: echo foo.{a,b,c} # == foo.a foo.b foo.c How do we get roughly: arr=(a b c) echo foo.{${arr[@]}} # == foo.a foo.b foo.c
user avatar
  • 131
0 votes
3 answers
504 views

bash counting script, counts fine ascending, doing error by counting descending

The follow bash script works fine for for high counting number ranges from 1..1000 By high counting number ranges, bigger than 1..1000000, it needs some time to start. In general, it's working fine. ...
user avatar
  • 11
4 votes
3 answers
1k 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[@...
user avatar
  • 441
0 votes
2 answers
325 views

Bash brace expansion with variables for pattern matching

I want to loop through all files matching a pattern of the form for file in {prefix1, prefix2, ...}*{postfix1, postfix2, ...}; do How can I programmatically replace {prefix1, prefix2, ...} with a ...
user avatar
3 votes
1 answer
2k views

rsync command doesn't work when run as a systemd service

summary: I am trying to set up a systemd timer to regularly backup a directory using the rsync command. I made an rsync command that works when run manually in a terminal, but it doesn't work ...
user avatar
  • 1,397
0 votes
2 answers
168 views

Why is Brace expansion with variables not working as expected? [duplicate]

I would like to set a variable (x=100) in the brackets such as x=100 for i in {0.."$x"..50} ;do echo $i done the desire output for i should be 0 50 100 although I get as an output {0..100.....
user avatar
0 votes
2 answers
260 views

How to create file names containing spaces when using brace expansion?

If this command: touch Test{1..3}.txt Allows me to create: Test1.txt Test2.txt Test3.txt What syntax would I need in order to batch create files containing a space in the file names? For example: ...
user avatar
1 vote
2 answers
164 views

Create multiples files with brace expansion combining letters and numbers

I am trying to create a series of files with brace expansions. I want create the files fileA1 to fileZ100 with all possible combinations (Something like touch file[A..Z][1..100]). If I run the ...
user avatar
  • 550
0 votes
1 answer
268 views

trying bash brace expansion (discrete numbers) using while read line and echo, doesn't expand

I'm going to give a short version of the kind of issue I'm facing. The real application I'm trying to write has many parts, so I've tried to isolate the part where I'm stuck with a small example. My ...
user avatar
2 votes
1 answer
672 views

how do I loop through selected file extensions in ZSH?

I am looking for a ZSH equivalent to this BASH construct: for file in *.{abc,def,ghi}; do #loop through all files with the extensions .abc, .def and .ghi I cannot find a similar construct for ZSH. ...
user avatar
  • 1,689
0 votes
3 answers
84 views

How do I join the results of a command substitution?

I want to create a list of mp3 files like a 1.mp3|a 2.mp3. I have tried echo ${(j:|:)$(echo *.mp3)} and some variations of it, but they don’t work: # too much splitting 01|The|Magic|Finger|01.mp3|...
user avatar
  • 1,287
2 votes
1 answer
223 views

Paste & Brace Expansion vs Wildcard

Why does a brace expansion behave differently than wildcard in combination with paste? Example: Assume we have multiple folders, each containing the same-structured tsv and want to create a 'all.tsv' ...
user avatar
  • 131
-1 votes
2 answers
32 views

bash: why can't I copy 3 files into 3 directories that share a name with the files?

So if I can do this: $ mkdir ./blah_{007,011,090} And end up with three directories... Why can't I do this... cp ../blah_{007,011,090}.swf ./blah_{007,011,090}_src And copy the files into 3 ...
user avatar
  • 4,039
3 votes
1 answer
379 views

Understanding fetching mutiple URLs and documents using curl

It's not clear to me how to fetch multiple documents using curl mainly because I do not fully understand the explanation in the curl documentation The curl documentation says: -o, --output <file&...
user avatar
  • 962
22 votes
8 answers
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?
user avatar
1 vote
1 answer
2k views

bash for loop multiple number ranges

I want to run a bash numeric 'for' loop but I want to skip some numbers in between. Example: for num in {1..4, 7..11, 23..34}; do (echo num $num); done or for num in {17..24, 41..48}; do (echo num ...
user avatar
1 vote
1 answer
1k views

Why does this rsync call not work properly?

I made a simple script to perform backup of my NAS. I do it as follows: date="$(date +%Y-%m-%d)" base="/backup" newest="$(ls -1td "$base"/*-*-*/ | head -n 1)" today="$base/$date" dirs="pub,data,...
user avatar
  • 430
1 vote
1 answer
723 views

cp runs from command line but file not found from script [duplicate]

I have a script to reorganize my directory structure that contains: files=./*"$pattern"I{"$ifirst".."$ilast"}.ext cp ${files} ../"$pattern"/"$i"/ When I run the script, I get an error: ./*S6I{001.....
user avatar
  • 11
28 votes
2 answers
4k 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?
user avatar
  • 2,996
6 votes
2 answers
3k 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 "...
user avatar
  • 185
2 votes
1 answer
99 views

Combine brace and variable expansion in one line

I would like to have a logging function that takes filenames as arguments and replicate the stdout to all of these files. This is what I have come up so far: function logger() { exec > >(...
user avatar
6 votes
1 answer
378 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 ...
user avatar
3 votes
2 answers
430 views

How do ranges in dash work?

I'm trying to figure out how to rush the following bash script in dash. for x in {1..5} do for y in {a..c} do echo $HOME$x$y done done Dash does not support brace expansion and I don'...
user avatar
8 votes
3 answers
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
user avatar
  • 679
0 votes
2 answers
287 views

Running multiple command against files matching a brace+glob pattern without repeating it

I would like to run a series of commands against a set of files matched by a braces-and-globs pattern, without copy-pasting the pattern all over the place. I've been trying to do this by putting the ...
user avatar
  • 1
0 votes
2 answers
305 views

Why do tilde prefixes expand prior to assignment, but braces don't

tl;dir In bash, brace expansion occurs before tilde expansion. However, tilde prefixes seem to expand prior to variable assignment and brace expansion doesn't. Why? Details and examples Brace ...
user avatar
  • 115
0 votes
1 answer
34 views

Can variable-substitution-syntaxes be unified?

Can these two variable-substitution-syntaxes combined into one? rm "$s_a/$domain.conf" 2>/dev/null rm "$s_e/$domain.conf" 2>/dev/null like: rm "$s_{a,e}/$domain.conf" 2>/dev/null
user avatar