Skip to content

Commit

Permalink
conventions.md: fix some other recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Jan 29, 2025
1 parent a2a32e5 commit 23000e5
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions docs/scripting/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,15 @@ echo-verbose "$a"

## Interpolation

Prefer `$var` rather than `${var}` to interpolate variables, unless doing so would otherwise complicate matters.
Prefer `$var` rather than `${var}` for simple interpolations, for advanced interpolations it is up to you.

```bash
# don't
local indent=' ' world='Earth'
echo "${indent}Hello, ${world}."
cat <<-EOF
Hello, ${world}.
EOF

# do
# recommended
local indent=' ' world='Earth'
echo "${indent}Hello, $world."
cat <<-EOF
${indent}Hello, $world.
EOF
echo "$world" # good
echo "${world}" # bad, unnecessary complexity
echo "${indent}Hello, $world." # fine
echo "${indent}Hello, ${world}." # also fine
```

Always use `"$HOME"` instead of `~`, as `~` doesn't work if it is inside a string, which becomes a common mistake when refactoring.
Expand Down Expand Up @@ -176,7 +169,8 @@ echo $"hello\n$name"
# which is is still not desire

# so let's use this, which is the right technique for the right bits
echo 'hello'$'\n'"$name"
echo 'hello'$'\n'"$name" # fine
echo $'hello\n'"$name" # also fine
# which outputs:
# hello
# dorothy
Expand Down

0 comments on commit 23000e5

Please sign in to comment.