0

I'm encountering a weird issue with using ls to determine if some path is a folder. I'm trying to verify if a user-input string is a valid directory. I'm using ls -ld to check the input, and then compare the first character of the output to d.

I assumed the following code would work, but it doesn't. Other valid paths (that don't start with ~) DO work as strings.

#!/bin/bash
is_valid_dir() {
    echo "input=$1"
    ls_result=$(ls -ld $1)
    echo "$ls_result"
    if [[ ${ls_result:0:1} == d ]]; then
        echo "input is a directory"
        return 0
    else
        echo "input is not a directory"
        return 1
    fi
}
is_valid_dir /home/simon                # Works
is_valid_dir .                          # Works
is_valid_dir ../.                       # Works
is_valid_dir ~                          # Works
is_valid_dir ~/..                       # Works
is_valid_dir "/home/simon"              # Works
is_valid_dir "."                        # Works
is_valid_dir "../."                     # Works
is_valid_dir "~"                        # Doesnt Work!
is_valid_dir "~/.."                     # Doesnt Work!

Can somebody tell my why supplying a path relative to ~ will confuse ls when supplied with ""?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Simon M.
  • 11
  • 1
  • Or https://stackoverflow.com/q/41871596/3001761, also on https://unix.stackexchange.com/questions/151850/why-doesnt-the-tilde-expand-inside-double-quotes, https://askubuntu.com/q/1192981 – jonrsharpe Feb 28 '21 at 16:46
  • `The order of word expansion shall be as follows: Tilde expansion (see Tilde Expansion), parameter expansion (see Parameter Expansion), command substitution (see Command Substitution), and arithmetic expansion (see Arithmetic Expansion) shall be performed, beginning to end.` tilde expansion happens before parameter expansion – William Pursell Feb 28 '21 at 16:49
  • Thanks for the comments! It made totally clear what I did wrong! – Simon M. Feb 28 '21 at 17:45
  • Don't use `eval` -- it solves this problem, but opens a whole bunch of opportunities for other parsing problems. The best answer is to just not quote the `~` in the first place. If you need to accept literal tildes, then it's better to expand it yourself. But `eval` also expands *every other bit of shell syntax*, whether or not it makes any sense to. – Gordon Davisson Feb 28 '21 at 20:40
  • @GordonDavisson The problem with this is that I'm using `read` to prompt the user for a install path. When the user enters `~`, read apparently supplies the string as an unexpanded "~". If you have another/better suggestion I would love to hear it! – Simon M. Mar 02 '21 at 11:03
  • See [here](https://stackoverflow.com/questions/3963716/how-to-manually-expand-a-special-variable-ex-tilde-in-bash) and [here](https://stackoverflow.com/questions/15858766/tilde-expansion-in-quotes). In your situation, `path="${1/#~/$HOME}"` would probably be sufficient. – Gordon Davisson Mar 02 '21 at 20:08
  • Answers don't belong in questions (in part for the reason that caused an update: Questions "answered" that way can't have those answers separately voted on / commented on / etc distinct from the question itself, so there aren't good ways for the community to communicate to readers about the answer quality distinct from the question quality). Thus, for a question flagged duplicate, the answers belong (_as answers_) on the canonical questions in the duplicate list, not edited into the question itself. – Charles Duffy Mar 06 '21 at 15:06

0 Answers0