This is how I wrote a short snippet to open read-only postgres connection using psql. Notice the amount of non-trivial stuff in those lines (technically, only one line of code).
I tried to show many concepts in those short snippets I post around here.
- For example, notice how some lines need backslashes to continue the line, but some others not.
- Also notice how to use <() to create a temporary file, so we don't have to have a duplicate file (probably outdated).
- The command being a "cat originalfile && echo "customline") makes it a fresh file each time, that inherits from your originalfile.
- Finally, good old "$@" to proxy the parameters to another command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ropsql() { | |
PSQLRC=<( | |
cat ~/.psqlrc && | |
echo 'set session characteristics as transaction read only;') \ | |
psql "$@" | |
} |
in zsh, you can add "compdef ropsql=psql" so it also proxies the autocompletion