11 ms·
Small improvements: - No quotes needed - Just type 'notes' alone to less the file notes() { if [ "$1" == "" ]; then less $HOME/note
by nsb1 9y ago
Small improvements:
- No quotes needed
- Just type 'notes' alone to less the file
notes() {
if [ "$1" == "" ]; then
less $HOME/notes.md
else
echo $@ >> $HOME/notes.md
fi
}
- vacri 9y agoCombination of your improvements and the first commentor's in the link - 'notes' by itself views the file (interactive shell only) - 'notes' with args, the args are appended to file (original function) - 'notes' with heredoc (or any piped data) allows for multiline notes notes() { if [ -n "$1" ]; then echo $@ >> "$HOME/notes.md" else # detect if we're in a tty or a pipe if [ -t 0 ]; then less "$HOME/notes.md" else cat - >> "$HOME/notes.md" fi fi } usage: $ notes 1 $ notes 1 2 3 $ notes <<EOF > this is a > multiline note > EOF $ uptime | notes $ notes (opens less with the following content) 1 1 2 3 this is a multiline note 23:22:06 up 3 days, 11:41, 1 user, load average: 0.25, 0.15, 0.26
- aidenn0 9y agoYou should use dollar-star and printf to prevent echo treating some of the arguments as flags: printf '%s\n' "$*" Which works while echo "$@" might not work as expected if you did e.g.: notes -n is a bad flag to pass to some commands The latest posix bans echo from taking any - options, so you may be safe with /bin/echo or if your shebang specifies "sh" but that hasn't percolated down everywhere yet.
- hk__2 9y agoYou still need quotes to prevent the shell from expanding $variables and globs. It’ll also mangle your spaces (try with `notes foo<space><space>bar`: it saves it as "foo<space>bar") due to the $@.