logoalt Hacker News

paulddraper • today at 2:19 PM • 2 replies • view on HN

People quote both too often and too little.

GENERAL RULE

1. Double-quote dollar sign expressions, and nothing else.

  foo

  "$bar"/foo

  baz:"$(cat example.txt)"

  exec cmd "$@"
2. Single-quote words with a literal special character, and nothing else.

  'Die Hard'

  'ke$ha'
---

I should point out that the author's example is NOT fixed by different quoting though.

  # original
  export PATH="$PATH:~/.local/bin/"

  # without unnecessary quotes
  export PATH="$PATH":~/.local/bin/
Because tilde expansion only happens at the beginning of the word.

Replies

gray_-_wolf • today at 7:09 PM

> I should point out that the author's example is NOT fixed by different quoting though.

Sure, but I would say you almost always want to add to the start of the PATH, not to the end.

➕ show 2 replies
JdeBP • today at 3:21 PM

The Z shell's, C shell's, and others's syntaxes for setting the PATH environment variable via a shell array variable alias also does the tilde expansion.

    path=( $path ~/bin )
It's worth noting, also, that the path and manpath settings in login.conf(5) expand leading tildes in individual search path items.

* https://man.freebsd.org/cgi/man.cgi?query=login.conf&sektion...

So putting the addition of things like ~/bin to PATH in /etc/login_conf and ~/.login_conf instead of shell scripts is another way to address it.

    :path=~/bin /usr/local/bin /usr/pkg/bin /usr/bin /bin:
It's particularly handy when there are multiple login shells in use.

* http://jdebp.uk./FGA/BSDs-for-Linux-users/login-conf.html