2

Bash is not expanding the ~ character in the argument --home_dir=~. For example:

$ echo --home_dir=~
--home_dir=~

Bash does expand ~ when I leave out the hyphens:

$ echo home_dir=~
home_dir=/home/reedwm

Why does Bash have this behavior? This is irritating, as paths with ~ are not expanded when I specify that path as an argument to a command.

2
  • 1
    Cheat mode: echo --home_dir=$(echo ~) . Not quite as pretty as what you're looking for, but might help more generically. e.g., echo --bin_dir=$(echo ~/bin)
    – Tanktalus
    Aug 6, 2018 at 19:09
  • Simply quoting the argument is a lot easier and more efficient than invoking an unnecessary command substitution.
    – chepner
    Aug 6, 2018 at 19:12

3 Answers 3

3

bash is somewhat mistakenly treating home_dir=~ as an assignment. As such, the ~ is eligible for expansion:

Each variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =. In these cases, tilde expansion is also performed.

Since --home_dir is not a valid identifier, that string is not mistaken for an assignment.

Arguably, you have uncovered a bug in bash. (I say arguably, because if you use set -k, then home_dir=~ is an assignment, even though it is after, not before, the command name.)


However, when in doubt, quote a string that is meant to be treated literally whether or not it is subject to any sort of shell processing.

echo '--home_dir=~'

Update: This is intentional, according to the maintainer, to allow assignment-like argument for commands like make to take advantage of tilde-expansion. (And commands like export, which for some reason I was thinking were special because they are builtins, but tilde expansion would have to occur before the actual command is necessarily known.)

2

Like chepner says in their answer, according to the documentation, it shouldn't expand it even in echo home_dir=~. But for some reason it does expand it in any word that even looks like an assignment, and has done so at least as far back as in 3.2.

Most other shells also don't expand the tilde except in cases where it really is at the start of the word, so depending on it working might not be such a good idea.

Use "$HOME" instead if you want it to expand, and "~" if you want a literal tilde. E.g.

$ echo "~" --foo="$HOME"
~ --foo=/home/itvirta

(The more complex cases are harder to do manually, but most of the time it's the running user's own home directory one wants.)

2

Well, that's because in echo --home_dir=~, the '~' does not begin the word and the output of echo is not considered a variable assignment. Specifically, man bash "Tilde Expansion" provides expansion if

  • If a word begins with an unquoted tilde character (~); or
  • variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =.

You case doesn't qualify as either.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.