this post was submitted on 10 May 2025
12 points (100.0% liked)
Shell Scripting
1442 readers
3 users here now
From Ash, Bash and Csh to Xonsh, Ysh and Zsh; all shell languages are welcome here!
Rules:
- Follow Lemmy rules!
- Posts must relate to shell scripting. (See bottom of sidebar for more information.)
- Only make helpful replies to questions. This is not the place for low effort joke answers.
- No discussion about piracy or hacking.
- If you find a solution to your problem by other means, please take your time to write down the steps you used to solve your problem in the original post. You can potentially help others having the same problem!
- These rules will change as the community grows.
Keep posts about shell scripting! Here are some guidelines to help:
- Allowed: Release Announcement of a command-line program designed for scripted use (e.g. bash, bats, awk, jq, coreutils/moreutils)
- Allowed: Tutorials on using shell languages or supplementary tools designed for scripted use
- Allowed: Code review/help requests for shell languages or supplementary tools designed for scripted use
- NOT Allowed: Announcement of a CLI or TUI program that is not designed for scripted use (Yes, your fancy Zsh plugin which pretty-prints the date and time using only builtins is very cool, but unless you actually want to discuss the code itself, please check out !commandline instead!)
- NOT Allowed: Domain-specific tutorials that do not involve shell scripting as a core component (e.g. docker-compose, ansible, nix). If you really love one of these, I'm sure there's a community out there ready to talk about them!
- NOT Allowed: Code review requests for non-shell programming languages and configuration languages (e.g. Python, Yaml)
In general, if your submission text is primarily shell code, then it is welcome here!
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I don't think that works, because the command substitution in
"$(…).txt"
runs immediately in the current shell.Aside from that,
find -exec
doesn't use a shell to run its command, which means$(…)
won't work without an explicit sh call.I believe the right command in this style that will work is:
However, I would recommend the
for f in *.mp3
-style solution instead, as to me it's more readable. (The Bash/Zsh recursive glob (**
) syntax can be used if subdirectories are involved.)You're correct about command substitutions, the
$(...)
part. I had initially thought putting it inside ash
would be clearer and avoid problems with substitutions. However,$0
is the name of the shell or the script. To fix this, we can put{}
inside a variable, like this:file="{}"
. Then, we can use the variable$file
for the rest of the command.I also think using
for
loops makes the command easier to read. But dealing with files that have spaces in their names can be really frustrating when you usefor
loops.