Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
0
answers
38
views
bash subshell execution behaves unexpectedly
I have a script which is supposed to fetch 2 URLs sequentially: #!/bin/bash wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/' $($wget_command) echo $wget_command >> wget_commands.log results in this message: --2025-04-30 09:13:49-- https://www.example.com/%7Bpath1...
I have a script which is supposed to fetch 2 URLs sequentially:
#!/bin/bash
wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/ '
$($wget_command)
echo $wget_command >> wget_commands.log
results in this message:
--2025-04-30 09:13:49-- https://www.example.com/%7Bpath1,path2%7D/
^ the
%7D
is a problem
----------
Whereas, directly issuing:
wget --restrict-file-names=unix https://www.example.com/{path1,path2}/
results in the expected fetches and messages:
--2025-04-30 09:14:13-- https://www.example.com/path1/
...
--2025-04-30 09:14:14-- https://www.example.com/path2/
----------
It feels like {path1,path2}
is triggering an expansion/interpolation when $($wget_command)
goes to use it but I am not sure how to verify nor avoid this problem.
MonkeyZeus
(143 rep)
Apr 30, 2025, 01:30 PM
1
votes
0
answers
25
views
zsh - autocomplete with braces in the middle of a directory
Suppose I have the following directory structure: ``` folder/ aaa/ f.txt bbb/ f.txt ``` I want to compare the file `f.txt` as it is common to both directories. So in zsh I type this: ``` % diff folder/{aaa,bbb}/ ``` Pleasingly, zsh autocompletes both the `aaa` and `bbb` directories even inside the b...
Suppose I have the following directory structure:
folder/
aaa/
f.txt
bbb/
f.txt
I want to compare the file f.txt
as it is common to both directories. So in zsh I type this:
% diff folder/{aaa,bbb}/
Pleasingly, zsh autocompletes both the aaa
and bbb
directories even inside the brace. Next, I press the Tab key hoping that zsh will autocomplete any files common to both the aaa
and bbb
directories, so I'm hoping that pressing Tab will result in this:
% diff folder/{aaa,bbb}/f.txt
However, what zsh actually does is expand the braces resulting in this:
% diff folder/aaa/ folder/bbb/
My questions are:
1) Is there a zsh setting(s) to keep the braces in the middle of the directory and autocomplete the common file name as I described?
2) If not, is there a way to write a custom function to achieve this?
Trevor
(1739 rep)
Mar 6, 2025, 06:19 AM
• Last activity: Apr 1, 2025, 09:28 PM
9
votes
1
answers
658
views
Why does bash give the following result after brace expansion?
I am using linux and the following version of the bash: ``` GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) ``` When I type: ``` echo file{[1,2],3}.txt ``` I expect brace expansion to be done first, so: ``` echo file[1,2].txt file3.txt ``` After that, I expect it to stop there, since [1,2]...
I am using linux and the following version of the bash:
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
When I type:
echo file{[1,2],3}.txt
I expect brace expansion to be done first, so:
echo file[1,2].txt file3.txt
After that, I expect it to stop there, since [1,2] is not a valid globbing. However, it gives the following as output:
file[1.txt file2].txt file3.txt
Why?
List of files in current directory (result of ls
command):
go.mod hello.sh hi main.go
Yakog
(517 rep)
Feb 22, 2025, 01:46 PM
• Last activity: Feb 24, 2025, 12:24 PM
6
votes
2
answers
12108
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 deliver the expanded version to the executed program? If so, what other tricks can it do and is...
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 deliver the expanded version to the executed program?
If so, what other tricks can it do and is there a specification?
Also, is there a name for it?
Is ls *.txt
handled in a similar way internally?
Is there a way to achieve an n-times repetition of an argument? Like (not working, of course, only a concept): cat test.pdf{*3}
⇒ cat test.pdf test.pdf test.pdf
?
Matmarbon
(265 rep)
Dec 6, 2017, 12:42 AM
• Last activity: Nov 8, 2024, 01:49 AM
29
votes
2
answers
4540
views
What is the difference between `a[bc]d` (square 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`?
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
?
Weijun Zhou
(3548 rep)
Apr 27, 2019, 05:35 PM
• Last activity: Sep 6, 2024, 03:15 AM
2
votes
2
answers
180
views
Repeating zsh brace expansion values in zsh to download multiple files using wget2
I want to download all files named `${n}x${n}` from a directory on a website with wget2 on zsh, where `n` is the same number value both times, with n from 1 to 6000. I've found that specifying all the download URLs as arguments to a single call to `wget2` works much faster than a for loop calling `w...
I want to download all files named
${n}x${n}
from a directory on a website with wget2 on zsh, where n
is the same number value both times, with n from 1 to 6000.
I've found that specifying all the download URLs as arguments to a single call to wget2
works much faster than a for loop calling wget2
once per URLs.
I assume I could use brace expansion to populate an array, then modify the array elements to produce the correct 6,000 URLs with the number repeated, then pass the array as the arguments to wget2
, but I'd prefer a more concise methodology, if possible.
Maybe a one-liner with brace expansion could work. I don't know, however, how to repeat a brace expansion value.
e.g.:
wget2 -q https://example.com/{1..6000}x
# like:
# wget2 -q https://example.com/1x1
# wget2 -q https://example.com/2x2
# …
How can I repeat a brace expansion value? Is there some other way to do this efficiently without brace expansions or without having to go through all the steps of creating an array?
XDR
(451 rep)
Aug 26, 2024, 09:14 PM
• Last activity: Aug 27, 2024, 09:25 AM
73
votes
7
answers
94036
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 may be obtained using `seq` (didn't try), but that is not what I am looking for. Useful info may be that...
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 may be obtained using
seq
(didn't try), but that is not what I am looking for.
Useful info may be that I am restricted to this bash version. (If you have a zsh
solution, but no bash
solution, please share as well)
$ bash -version
GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)
Bernhard
(12573 rep)
Jan 4, 2013, 08:41 AM
• Last activity: Jul 1, 2024, 01:52 PM
0
votes
1
answers
114
views
Bash brace expansion doesn't work in an export statement
Consider the following example: ``` $ echo file_{a,b,c} file_a file_b file_c # brace expansion worked :) $ export VARIABLE=file_{a,b,c} $ echo $VARIABLE file_c # brace expansion didn't work :( ``` How come that if I echo `file_{a,b,c}` directly, the brace expansion prints the name of all three files...
Consider the following example:
$ echo file_{a,b,c}
file_a file_b file_c # brace expansion worked :)
$ export VARIABLE=file_{a,b,c}
$ echo $VARIABLE
file_c # brace expansion didn't work :(
How come that if I echo file_{a,b,c}
directly, the brace expansion prints the name of all three files, while if I assign the same expression to a variable and echo that, it only prints the name of file_c?
And is there a different way to assign a brace-expanded string to a variable that would work in my case?
Thomas Fritz
(185 rep)
Feb 5, 2024, 12:31 PM
• Last activity: Feb 5, 2024, 01:24 PM
5
votes
4
answers
853
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` will match The word `car` will also match The word `carat` will **not** match However, if `a,a,c...
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
will match
The word car
will also match
The word carat
will **not** match
However, if a,a,c,r,t
were given
Then carat
would have matched.
I am trying to find if it is possible only using grep
, I suspect that brace expansions like
{a,c,r,t}{a,c,r,t}
might be useful to generate all the possible combinations of the letters but instead I am greeted with the errors like
grep: aaac: No such file or directory
grep: aaar: No such file or directory
grep: aaat: No such file or directory
when running the command
$ grep {a,c,r,t}{a,c,r,t}{a,c,r,t}{a,c,r,t} /usr/share/dict/words
When I use quotes like "{a,c,r,t}{a,c,r,t}"
or "\{a,c,r,t\}\{a,c,r,t\}"
, brace expansion does not work at all
I know that the above command should not work as a scrabble helper but the errors are still rather unexpected. What is wrong with the command and how do I fix it? Also, can grep
be used some way to make a scrabble helper at all?
AvZ
(233 rep)
Dec 21, 2015, 08:35 AM
• Last activity: Feb 2, 2024, 08:22 PM
5
votes
2
answers
9687
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/raspcondo/webcam/ ``` These do not: ``` rsync -Pa --rsh="ssh -p 19991" --exclude={'*.jpg', '*.mp4'} pi@localhost...
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/raspcondo/webcam/
These do not:
rsync -Pa --rsh="ssh -p 19991" --exclude={'*.jpg', '*.mp4'} pi@localhost:/home/pi/webcam /Volumes/Media/Webcam\ Backups/raspcondo/webcam/
rsync -Pa --rsh="ssh -p 19991" --exclude {'*.jpg', '*.mp4'} pi@localhost:/home/pi/webcam /Volumes/Media/Webcam\ Backups/raspcondo/webcam/
This is the output:
building file list ...
rsync: link_stat "/Users/mnewman/*.mp4}" failed: No such file or directory (2)
rsync: link_stat "/Users/mnewman/pi@localhost:/home/pi/webcam" failed: No such file or directory (2)
0 files to consider
sent 29 bytes received 20 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files could not be transferred (code 23) at /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-54.120.1/rsync/main.c(996) [sender=2.6.9]
What am I doing wrong with the list of file types to exclude?
Buadhai
(257 rep)
May 26, 2021, 02:17 AM
• Last activity: Jan 27, 2024, 03:47 AM
1
votes
1
answers
252
views
mkdir -p dir with braces created wrongly
I executed the following code in Ubuntu server 16.04 xenial: mkdir -p /root/backups/{db, dirs} I recall that in another system, it worked like charm creating all 3 dirs: /root/backups/ /root/backups/db /root backup/dirs Yet this time the result was: /root/backups/ /root/backups/{db, Why is this part...
I executed the following code in Ubuntu server 16.04 xenial:
mkdir -p /root/backups/{db, dirs}
I recall that in another system, it worked like charm creating all 3 dirs:
/root/backups/
/root/backups/db
/root backup/dirs
Yet this time the result was:
/root/backups/
/root/backups/{db,
Why is this partial, broken result?
Arcticooling
(1 rep)
Jan 9, 2018, 04:35 PM
• Last activity: Sep 8, 2023, 07:59 AM
0
votes
2
answers
366
views
Pass a variable that contains a comma as a -v option to qsub
After seeing the reactions on [Stack Overflow](https://stackoverflow.com/questions/76616176/pass-a-variable-that-contains-a-comma-as-a-v-option-to-qsub) on this question and an unfamiliarity with qsub, I believe thqt U&L is better suited for this question. In qsub, we can pass environment variables...
After seeing the reactions on [Stack Overflow](https://stackoverflow.com/questions/76616176/pass-a-variable-that-contains-a-comma-as-a-v-option-to-qsub) on this question and an unfamiliarity with qsub, I believe thqt U&L is better suited for this question.
In qsub, we can pass environment variables (a comma-separated list of
envar=value
pairs) like so:
info="This is some info"
qsub -v INFO=$info script.pbs
However, this becomes problematic when $info contains a comma.
info="This is some info, and here is some more!"
qsub -v INFO=$info script.pbs
This will trigger an error like so:
> ERROR: -v
: variable ' and here is some more!' is not set in environment variables.
I have also tried encapsuling info
, INFO="$info"
leading to the same issue.
How can I pass $info
correctly, even if it contains one or more commas? The same question holds with newlines which always end up incorrectly when they are passed (backslash gets removed).
Perhaps an interesting observation is that when I echo -e $info
I get the output that I expect. The error is triggered in the qsub
command specifically.
Bram Vanroy
(183 rep)
Jul 4, 2023, 11:24 PM
• Last activity: Jul 6, 2023, 07:59 AM
0
votes
2
answers
134
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 will help to execute this?
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 will help to execute this?
Elena
(1 rep)
Apr 29, 2021, 01:36 AM
• Last activity: Mar 11, 2023, 07:48 PM
9
votes
2
answers
2906
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-clydsedale > moose-hoof ovary clydsedale What are the right expansion flags to allow string concatenation o...
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-clydsedale
> moose-hoof ovary clydsedale
What are the right expansion flags to allow string concatenation on each element of the array?
alex gray
(291 rep)
Jan 31, 2016, 07:40 PM
• Last activity: Jan 21, 2023, 04:33 PM
3
votes
3
answers
817
views
Brace expansion to run program multiple times with different arguments
I just learned about brace expansion and hoped I could make use of them to launch the same C++ program with different command line arguments. My code is run like this from the terminal: ``` mpirun -n 1 main.exe 1 10 0.1 1 5 ``` The numbers after main.exe are the input arguments of my program. I woul...
I just learned about brace expansion and hoped I could make use of them to launch the same C++ program with different command line arguments.
My code is run like this from the terminal:
mpirun -n 1 main.exe 1 10 0.1 1 5
The numbers after main.exe are the input arguments of my program.
I would like to do something like this:
mpirun -n 1 main.exe 1 10 {0.1,0.2} 1 5
where I expect the code to be run twice, once with 0.1 and once with 0.2 as the third argument.
Why does it not work and how can I fix it?
Best
reloh100
(29 rep)
Jan 9, 2023, 03:31 PM
• Last activity: Jan 10, 2023, 06:46 PM
0
votes
1
answers
285
views
Bash - for range no longer works as wanted when one of the ends is a variable
``` #!/bin/bash myfirstarray=(1 3 5 7 9 11) for i in {2..4} do for j in {1..${myfirstarray[$((i-1))]}} do echo ${j} done done ``` In the code above the range of the outer loop is interpreted as wanted, iterating from ```2``` to ```4```. Both substitutions in the range of the inner loop also work. Ho...
#!/bin/bash
myfirstarray=(1 3 5 7 9 11)
for i in {2..4}
do
for j in {1..${myfirstarray[$((i-1))]}}
do
echo ${j}
done
done
In the code above the range of the outer loop is interpreted as wanted, iterating from
to
. Both substitutions in the range of the inner loop also work. However the inner range is interpreted as a string and not a range of integers. How can I fix this issue?
Expected:
1
2
3
1
2
3
4
5
1
2
3
4
5
6
7
Result:
{1..3}
{1..5}
{1..7}
wing47299
(1 rep)
Dec 19, 2022, 07:51 AM
• Last activity: Dec 19, 2022, 10:50 AM
4
votes
1
answers
406
views
Why am I not able to use * with touch in path?
This is the output of `tree`: ``` [xyz@localhost Semester1]$ tree . ├── Eng ├── IT ├── IT_workshop ├── LA ├── OS ├── OS_lab ├── Psy ├── Python └── Python_lab 9 directories, 0 files ``` I want to create 3 common files (named credits, links and notes) in each of these directories using `touch`. I trie...
This is the output of
tree
:
[xyz@localhost Semester1]$ tree
.
├── Eng
├── IT
├── IT_workshop
├── LA
├── OS
├── OS_lab
├── Psy
├── Python
└── Python_lab
9 directories, 0 files
I want to create 3 common files (named credits, links and notes) in each of these directories using touch
.
I tried this command:
[xyz@localhost Semester1]$ touch */{credits,links,notes}
and this was the output:
touch: cannot touch ‘*/credits’: No such file or directory
touch: cannot touch ‘*/links’: No such file or directory
touch: cannot touch ‘*/notes’: No such file or directory
Why did the command not work as I expected it to?
BTW, I'm using CentOS Linux 7.
Random Person
(311 rep)
Nov 4, 2022, 03:09 PM
• Last activity: Nov 4, 2022, 05:25 PM
61
votes
7
answers
19676
views
When do you use brace expansion?
I understand what [brace expansion][1] 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. [1]: http://www.gnu.org/software/bash/manual/bashref.html#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.
Benjamin
(1515 rep)
Jan 18, 2011, 12:34 PM
• Last activity: Jun 22, 2022, 02:50 PM
8
votes
2
answers
3334
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 `path2` exists: ``` ls -d /some/{path1,path2}/* ``` But if I do the exact same thing in `...
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 path2
exists:
ls -d /some/{path1,path2}/*
But if I do the exact same thing in bash
, I get 2
exit code, with a stderr
message reporting path1
does not exist (if path1 is the one that doesn't exist).
How can I make bash
behave like tcsh
in this case? Is there a switch to ls
that I can ask it to give back 0
if at least one path exists? If neither one exists, I do expect non-zero code, which is what tcsh
gives back.
shikhanshu
(245 rep)
Mar 5, 2022, 06:16 AM
• Last activity: Jun 14, 2022, 03:47 PM
2
votes
1
answers
564
views
How to preserve parameter expansion passed to a function?
I have this function: ``` cyan=`tput setaf 6` reset=`tput sgr0` function Info() { echo "${cyan}$1${reset}" } ``` And I use it in my other scripts as simple as `Info some message`. However, when I use it to print all items of an array, it only prints the first item: ``` Info "${ArrayVariable[@]}" # t...
I have this function:
cyan=tput setaf 6
reset=tput sgr0
function Info()
{
echo "${cyan}$1${reset}"
}
And I use it in my other scripts as simple as Info some message
.
However, when I use it to print all items of an array, it only prints the first item:
Info "${ArrayVariable[@]}" # this only prints the first item
echo ${ArrayVariable[@]}" # this prints all of them
How can I preserve all of the variables when using this syntax and this function?
Saeed Neamati
(841 rep)
May 21, 2022, 10:19 AM
• Last activity: May 21, 2022, 12:06 PM
Showing page 1 of 20 total questions