-1

I'm new to Linux as well as bash. I made a script that accesses a folder that is located in the home directory, but the script will not always be called from the home directory. The prompt I'm getting when calling it from any subdirectories specifies that it can not find the file.

Here's an example of what I'm trying to do:

for entry in "~/.directory"/*
do
    echo "$entry"
done

If I place the script in a subdirectory of /home and try to call it, the script is unable to find the directory. I know it exists as if I run ls ~/.directory in the subdirectory it is able to find the files and print them with no problem. Is there a different way I should be trying to access the directory in the bash shell? Thanks!

Voted to close my question. It seems rather specific to me, and the general solution was something I found earlier and was also posted in the comments below. I'll figure it out eventually -

TEEBQNE
  • 6,104
  • 3
  • 20
  • 37
  • Use `$HOME` instead. – Mark Setchell Oct 01 '18 at 20:55
  • @MarkSetchell I changed the line to 'for entry in "$HOME/.directory"/* but am still getting the same print out specifying: "cannot access '.directory' : No such file or directory". – TEEBQNE Oct 01 '18 at 20:57
  • Maybe you haven't got a directory called `$HOME/.directory`. Try `ls -ld $HOME/.*` – Mark Setchell Oct 01 '18 at 20:58
  • @user3657449 That sounds like there's a problem with the `HOME` variable. What does `echo "'$HOME'" | LC_ALL=C cat -vt` print? – Gordon Davisson Oct 01 '18 at 21:03
  • @MarkSetchell The script runs fine and recognizes the $HOME variable when the script is called from the home directory. I tried ls -ld $HOME/.* and the directory I am looking for in the script appears. – TEEBQNE Oct 01 '18 at 21:03
  • @GordonDavisson Oh it appears it does not exist. It prints bash: '/home/user': No such file or directory – TEEBQNE Oct 01 '18 at 21:04
  • @GordonDavisson Sorry I was wrong. I missed the 'echo' in your prompt. It prints '/home/user' – TEEBQNE Oct 01 '18 at 21:10
  • @BenjaminW. I found that earlier. Tried it and still had no luck with the directory being found. – TEEBQNE Oct 01 '18 at 22:06
  • Well, the quoted tilde is definitely at least part of the problem. It's odd that `$HOME` contains `/home/user` for you – it's typically `/home/user/`. – Benjamin W. Oct 01 '18 at 22:09
  • @BenjaminW. It is. This question is very specific. As it most likely does not affect a lot of other people it seems rather off-topic for stack overflow. I might just close it and try to figure this out myself. – TEEBQNE Oct 02 '18 at 00:13
  • When you're in the directory where your script lives, what's the output of `echo "$PWD"`? – Benjamin W. Oct 02 '18 at 02:49

1 Answers1

1

Only unquoted tildes are expanded.

for entry in ~/".directory"/*
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I tried moving the tilde outside of the quotes but still didn't find the directory. – TEEBQNE Oct 01 '18 at 20:11
  • Are you getting an actual error, or does the loop you show simply produce no output? If your script had DOS line endings, for instance, your pattern would have a carriage return following the `*`, which means your pattern would probably not match anything. – chepner Oct 01 '18 at 20:19
  • I get 'ls: cannot access '.directory': No such file or directory'. – TEEBQNE Oct 01 '18 at 20:29
  • @user3657449 You need to move both the `~` and `/` outside the quotes -- `~/".directory"/*` will work, but `~"/.directory"/*` will not. BTW, are there any shell metacharacters (whitespace, wildcards, etc) in the quoted section? If not, you can just leave the quotes off entirely. – Gordon Davisson Oct 01 '18 at 20:58
  • I moved both the slash and tilde outside of the quotations. I will try removing the quotations. There are no whitespaces nor wildcards in the name of the directory. – TEEBQNE Oct 01 '18 at 21:00