bjorn.now

#shell-scripting

Using various shell to script your day-to-day, likely a lot of bash or sh

09 October, 2025

Don’t quote regex patterns in Bash [[ ]] tests, because they’ll match literally.

So the below won’t match because it’s looking for a literal $ in the string, instead of matching at the end of the string:

file="https://example.com/blog-post/index.html"
[[ "$file" =~ "/index.html$" ]] && echo MATCH

But this will:

file="https://example.com/blog-post/index.html"
[[ "$file" =~ /index\.html$ ]] && echo MATCH

Note: [[ ]] is a Bash extension of the test command that supports regexes (among other things). The [ ] is the POSIX standard test (see man test), so [[ ]] features may not work in other shells or in limited shells like ash or sh.

[… more]
til 1 min read #bash, #regex, #shell-scripting

20 August, 2025

15 August, 2025

If you start a non-interactive bash shell it will source the content of the file defined in BASH_ENV (and ENV for a POSIX shell).

[… more]
til 1 min read Singapore • #shell-scripting

13 August, 2025

/usr/bin/env executes commands with flags/subcommands, not just bare executables. Which is great if you, for example, have a script/lint that’s a Python script, and it needs dependencies from a virtualenv that isn’t active when you call it.

Just put your shebang as /usr/bin/env uv run python3 and it always runs in the virtualenv, no wrapper script needed. This feels obvious in hindsight, it’s what you expect from these tools 😃

til Singapore • #shell-scripting

02 August, 2025

In shell scripts if you do "$@" it will actually expand “quoted sentences” correctly, and if you just do $@ it will always unwrap them into single words, I thought that if you did "$@" it would combine all arguments into a single argument, and what it does is do what I thought $@ alone did.

I.e., with "$@" the arguments "one two" three will be 2 arguments, the first being "one two", and without it will become three arguments, all separated by space.

til Singapore • #shell-scripting

The scripts/commands inside a shell script inherit access to STDIN when you call them, so if you have a shells script that only has cat and you do ./script.sh < script.sh then it’ll output the content of itself

til Singapore • #shell-scripting