logoalt Hacker News

amarshall • today at 1:46 PM • 6 replies • view on HN

Or you can just…not quote the tilde. Folks always seem to reflexively quote “strings” in Bash while not realizing that (almost) everything is a string and most strings are not quoted and it would be odd to do it (e.g. no one is doing `"ls" "-a" "foo"`).


Replies

Cockbrand • today at 2:54 PM

I feel like this is common knowledge and should not be worth mentioning. But then it apparently is not common knowledge, as the article proves.

Have I run into this at some point?

I certainly have.

Have I learned to quote better and only where appropriate from it?

I certainly have.

Bourne compatible shells take a while to learn and require some experience. This won't change, but alternatives exist, with their own caveats.

dylan604 • today at 2:06 PM

Unless you're running a shell command from python. That was the first time I saw a command string broken down into "string" arguments for every thing like that.

➕ show 1 reply
paulddraper • today at 2:19 PM

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.
➕ show 1 reply
vips7L • today at 2:08 PM

Or just stop using bash. It’s a terrible language to write and has tons of footguns.

➕ show 3 replies
cr125rider • today at 2:02 PM

The trick is to quote explicitly and correctly. “ and ‘ are different.

➕ show 1 reply
chasil • today at 4:10 PM

I think the appropriate method by POSIX rules would be:

  export PATH="$PATH:"~/.local/bin
I may be wrong. If I'm not, that works in any POSIX-compliant shell.

Edit: this appears to work properly with mksh on my phone:

  :/ $ export PATH="$PATH:"~/.local/bin
  :/ $ print $PATH
  /product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin:~/.local/bin
➕ show 1 reply