1

For example if I type ~ or ~/Documents as an input I get the message : No such file or directory

However if I use /home/username/Documents it works fine.

echo "Dose onoma katalogou"

read Directory

find $Directory -type f -perm 777

Any idea on why this happens and how I could fix it in order to be able to type pathnames including "~"?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MattSt
  • 1,024
  • 2
  • 16
  • 35

1 Answers1

1

bash expands the variable $Directory after it expands any ~, thus once $Directory is expanded, the time to expand ~ has passed.

eval find $Directory -type f -perm 777

will work because eval will see the ~ and run shell expansion again.

You may test the effect by simpler commands:

tilde='~'
echo $tilde            # prints a literal ~
eval echo $tilde       # prints your home directory

By the way, a directory names containing blanks will cause problems.

Hans Klünder
  • 2,176
  • 12
  • 8
  • 2
    Beware: `eval` is a very powerful mechanism — and very dangerous if the user controls the contents of `$Directory`. For example, if the user gets `Directory='$(rm -f $HOME &)'` into the `eval` command, someone will be very unhappy — especially if the script is run by `root`. – Jonathan Leffler Dec 28 '14 at 09:31