this post was submitted on 15 Jul 2023
27 points (100.0% liked)
Shell Scripting
1362 readers
1 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 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Nice! This one was probably the most likely to be found, I actually got this trick from somewhere else online before I understood what it was doing. If you're curious, I've got a few other lines of nested Zsh expansions I picked out but didn't post.
curious to see if AI can decipher? Certainly! bonus that we get so see potentially beneficial shell
Here's all the ones I was considering. I only posted the first two here. I'll add the explanations later.
spoiler
The given script appears to be written in the Zsh shell syntax. Let's break it down step by step:
set --
: This command sets the positional parameters of the script. In Zsh, the--
option is used to signify the end of options and the beginning of positional parameters."${@[0,eoo]:/(#m)^-*/${MATCH:a}}"
: This part refers to the expansion of the positional parameters, specifically targeting the elements from index 0 to the end of options offset (eoo
). Here's what the individual components mean:${@[0,eoo]}
: This refers to a sub-array of positional parameters from index 0 toeoo
. It extracts the elements corresponding to command-line options./(#m)^-*
: This is a pattern match within a substitution. It matches any occurrences of hyphens (-
) at the beginning of each element. The(#m)
flag enables multiline mode for pattern matching.${MATCH:a}
: This is the replacement part of the substitution. It expands the matched pattern (${MATCH}
) and applies the:a
modifier, which removes leading hyphens from the matched string.In summary, this part removes leading hyphens from the command-line options.
"${@[eoo+1,-1]:a}"
: This part refers to the expansion of the positional parameters, specifically targeting the elements from the end of options offset (eoo
+ 1) to the last element. Here's what the individual components mean:${@[eoo+1,-1]}
: This refers to a sub-array of positional parameters from the end of options offset (eoo
+ 1) to the last element. It extracts the arguments that come after the command-line options.:a
: This applies the:a
modifier, which removes leading hyphens from each element.In summary, this part removes leading hyphens from the command-line arguments that come after the options.
Overall, the script modifies the command-line options and arguments by removing leading hyphens. The modified values are then set as the new positional parameters using the
set
command. The exact purpose or context of the script can only be determined by understanding its broader context and usage.Corrections
It impressively identified exactly whateoo
meant, and handled the array splicing, but it tripped over Zsh's globbing syntax.:
in${foo:/pat/repl}
requirespat
to match the whole element^
actually negates the match, rather than anchors it to the start.^-*
matches any element not beginning with a hyphen.(#m)
flag captures the whole match in the parameter$MATCH
.${MATCH:a}
transforms relative paths toa
bsolute paths.Full context
I use this as a wrapper aroundnvim --remote
to make sure any non-options (i.e.: files) I pass have absolute paths rather than relative paths.