Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:53:32+00:00 2026-05-30T10:53:32+00:00

Here is the code, x= if [ -d $x ]; then echo it’s a

  • 0

Here is the code,

x=
if [ -d $x ]; then 
    echo "it's a dir"
else
    echo "not a dir"
fi

The above code gives me "it's a dir", why? $x is empty, isn’t it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:53:33+00:00Added an answer on May 30, 2026 at 10:53 am
    x=
    if [ -d $x ]; then
    

    is equivalent to:

    if [ -d ] ; then
    

    A simpler way to demonstrate what’s going on is:

    test -d ; echo $?
    

    which prints 0, indicating that the test succeeded ([ is actually a command, equivalent to test except that it takes a terminating ] argument.)

    But this:

    test -f ; echo $?
    

    does the same thing. Does that mean that the missing argument is both a directory and a plain file?

    No, it means that it’s not doing those tests.

    According to the POSIX specification for the test command, its behavior depends on the number of arguments it receives.

    With 0 arguments, it exits with a status of 1, indicating failure.

    With 1 argument, it exits with a status of 0 (success) if the argument is not empty, or 1 (success) if the argument is empty.

    With 2 arguments, the result depends on the first argument, which can be either ! (which reverses the behavior for 1 arguments), or a “unary primary” like -f or -d, or something else; if it’s something else, the results are unspecified.

    (POSIX also specifies the behavior for more than 2 arguments, but that’s not relevant to this question.)

    So this:

    x=
    if [ -d $x ]; then echo yes ; else echo no ; fi
    

    prints “yes”, not because the missing argument is a directory, but because the single argument -d is not the empty string.

    Incidentally, the GNU Coreutils manual doesn’t mention this.

    So don’t do that. If you want to test whether $x is a directory, enclose it in double quotes:

    if [ -d "$x" ] ; then ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, here is the code and then the discussion follows: public class FlatArrayList {
Here is my code... basically it finds any page-NUMBER- within a variable and then
Here's my code: if Request.Form(authorize) <> and request.form(delete) <> true then post_ids = Request.form(authorize)
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
Here is code from MSDN . I don't understand why the work isn't just
i see some code like this SERIAL_NO=121 if [ -z ${SERIAL_NO} ]; then echo
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
enter code here I have a table on SQL server 2005 with bigint primary
The code here is X++. I know very little about it, though I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.