0

I am using the standard check if a directory exists.

#!/bin/bash

if [ -d "~/.junk" ]; then
    echo "yeet"

[ -d "~/.junk" ] evaluates to false, however, when I cd into ~ and type ls -a, .junk is indeed one of the directories.

What could be happening?

I am using Linux Mint Vera

Ben Alan
  • 1,399
  • 1
  • 11
  • 27
  • 1
    Who is the user running that script .. Are you sure that `.junk` exists in `~/` for that user? Also `-d` will not work if `.junk` is a symlink. So a `ls -la` inside `~/` and see if it lists junk as a symlink and not a directory. – Zak Jan 26 '23 at 23:44
  • edit your Q to show the `ls -la ~/.junk` output. Good luck. – shellter Jan 27 '23 at 00:45

1 Answers1

1

when you use the "~" in 'string' such in this case, the character is not treated as a special character and is interpreted literally, so you should replace by $HOME

And change the param -d to -e

-e - True if the file exists (regardless of type).

-d - True if the file is a directory.

#!/bin/bash                                                                                                                                                                                                                                     
if [ -e "$HOME/.junk" ]; then                                                                                               
    echo "yeet"                                                                                                         
fi                                                                                                                                                                                                                                                                                                                                                                            
Beta
  • 41
  • 3
  • Not that you **can** use `~`, you just have to use it properly: `if [ -e ~/.junk ]` works fine. No need to revert to $HOME. If you are in a context, where quotes are advisable, `~` can be used to, as long as it stays outside the quotes: `~/"$MYDOTFILE"` for instance. – user1934428 Jan 27 '23 at 07:51