Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
What do the BASH file test operators return when the path argument is an empty string? For example:
directory="" # Something may or may not set "directory" if [ -d "$directory" ]; then # Do something... fi
You need to quote $directory:
$directory
if [ -d "$directory" ]; then
Otherwise, after expansion you are left with the equivalent of
if [ -d ]; then
which is equivalent to if [ -n "-d" ]; then, and so is always true. (-d is a non-empty string.)
if [ -n "-d" ]; then
-d
Add a comment
In your example, the statement will yield the following test:
if [ -d ]; then ... fi
Which will not test what you wish to.
What is the accepted practice to prevent such error is to encase the variable in double quotes like this:
if [ -d "$directory" ]; then ... fi
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.