this post was submitted on 15 Jul 2023
24 points (96.2% liked)
Shell Scripting
120 readers
4 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
That doesn't seem sensible. Moving the cursor will confuse bash and you can get the same effect by just omitting the last
\n
.Note that bash 5.0, but not earlier or later versions, is buggy with multiline prompts even if they're correct.
Your colors should use 39 (or 49) for reset.
Avoid doing external commands in subshells when there's a perfectly good prompt-expansion string that works.
You seem to be generating several unnecessary blank lines, though I haven't analyzed them in depth; remember that doing them conditionally is an option, like I do:
When I was testing it I did not get the same effect. Instead it would only put the background behind what I had typed and not the whole line. Doing it now it seems to be working with the omission. I would assume it's a terminal emulator bug because I believe I have changed emulators since I wrote it. I've now removed it, thanks for fixing a bug.
I wanted my home directory to not get shortened to
~
, and if there is some way to do that with\w
it isn't easy to find out how.Also, what's the reasoning for avoiding it (besides it being idiomatic)? I'm sure there is one, but I don't think I've run into it yet.
I just like the look of it, and I have the screen space to do it.
Two of the most expensive things a shell does are call
fork
and callexecve
for an external program.pwd
is a builtin (at least for bash) but the former still applies.$PWD
exists even if you don't want that shortening; just like your backticks be sure to quote it once so it doesn't get expanded when assigning to PS1.In general, for most things you might want to do, you can arrange for variables to be set ahead of time and simply expanded at use time, rather than recalculating them every time. For example, you can hook
cd
/pushd
/popd
to get an actually-fastgit
prompt. Rather thanvar=$(some_function)
you should havesome_function
output directly to a variable (possibly hard-coded -REPLY
is semi-common; you can move the value later);printf -v
is often useful. Indirection should almost always be avoided (unless you do the indirect-unset bash-specific hack or don't have any locals) due to shadowing problems (you have to hard-code variable name assumptions anyway so you might as well be explicit).