Skip to main content
added 278 characters in body
Source Link
Stéphane Chazelas
  • 574k
  • 96
  • 1.1k
  • 1.6k

The most recent POSIX specification for the cd utility (Issue 8, from 2024) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, at the time of writing (2025), this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving it up to the implementors of the standard to provide a useful consistent behaviour.

All shells I use daily (bash, zsh, OpenBSD ksh, ash) treat cd '' as cd . (or the first reachable directory in $CDPATH if set), while the (very much non-POSIX) fishrc (and derivatives) and fish shell generatesgenerate an error for cd ''.

The cd of csh/tcsh reports an error unless $cdpath in set in which case like on those shells mentioned above for $CDPATH, it chdir()s to the first reachable directory in that array. 

If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

The most recent POSIX specification for the cd utility (Issue 8, from 2024) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, at the time of writing (2025), this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving it up to the implementors of the standard to provide a useful consistent behaviour.

All shells I use daily (bash, zsh, OpenBSD ksh, ash) treat cd '' as cd ., while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

The most recent POSIX specification for the cd utility (Issue 8, from 2024) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, at the time of writing (2025), this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving it up to the implementors of the standard to provide a useful consistent behaviour.

All shells I use daily (bash, zsh, OpenBSD ksh, ash) treat cd '' as cd . (or the first reachable directory in $CDPATH if set), while the (very much non-POSIX) rc (and derivatives) and fish shell generate an error for cd ''.

The cd of csh/tcsh reports an error unless $cdpath in set in which case like on those shells mentioned above for $CDPATH, it chdir()s to the first reachable directory in that array. 

If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

added 13 characters in body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k

The most recent POSIX specification for the cd utility (Issue 8, from last year2024) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, at the time of writing (2025), this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving it up to the implementors of the standard to provide a useful consistent behaviour.

All shells that I use daily (bash, zsh, OpenBSD ksh, ash) accepttreat cd '' as the same as cd ., while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

The most recent POSIX specification for the cd utility (Issue 8, from last year) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving up to the implementors of the standard to provide a useful consistent behaviour.

All shells that I use daily (bash, zsh, OpenBSD ksh, ash) accept cd '' as the same as cd ., while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

The most recent POSIX specification for the cd utility (Issue 8, from 2024) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, at the time of writing (2025), this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving it up to the implementors of the standard to provide a useful consistent behaviour.

All shells I use daily (bash, zsh, OpenBSD ksh, ash) treat cd '' as cd ., while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

deleted 17 characters in body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k

The most recent POSIX specification for the cd utility (Issue 8, from last year) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving up to the implementors of the standard to provide a useful consistent behaviour.

All shells that I use daily (bash, zsh, OpenBSD ksh, ash) accept cd '' as the same as cd . without an operand, while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

The most recent POSIX specification for the cd utility (Issue 8, from last year) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving up to the implementors of the standard to provide a useful consistent behaviour.

All shells that I use daily (bash, zsh, OpenBSD ksh, ash) accept cd '' as the same as cd without an operand, while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

The most recent POSIX specification for the cd utility (Issue 8, from last year) has this in the OPERANDS section:

[...] If directory is an empty string, cd shall write a diagnostic message to standard error and exit with non-zero status. [...]

However, this is a fairly recent addition, and the previous issue of the standard said that it was "unspecified" what an empty operand would cause the utility to do, leaving up to the implementors of the standard to provide a useful consistent behaviour.

All shells that I use daily (bash, zsh, OpenBSD ksh, ash) accept cd '' as the same as cd ., while the (very much non-POSIX) fish shell generates an error for cd ''. If these shells aspire to be POSIX compliant (some do, others don't, but tend to align the general behaviour of built-in standard utilities to the common standard), I'm sure they will be updated over time to conform to the new specification.

In fact, the most recent development version of bash does this:

bash-5.3$ mydir=
bash-5.3$ cd "$mydir"
bash: cd: null directory
bash-5.3$ echo "$?"
1
bash-5.3$ echo "$BASH_VERSION"
5.3.0(1)-rc1

In the meantime, if this is important to your own scripts, you should test the value of the shell variable used as the operand to cd. A minimal thing to do could be to use something like cd "${mydir:?Empty or unset variable}" in your script:

$ mydir=
$ cd "${mydir:?Empty or unset variable}"
bash: mydir: Empty or unset variable
$ echo "$?"
1

... or do a separate sanity check on the variable before its use.

edited body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k
Loading
added 195 characters in body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k
Loading
added 60 characters in body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k
Loading
added 67 characters in body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k
Loading
added 350 characters in body
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k
Loading
Source Link
Kusalananda
  • 351.4k
  • 40
  • 726
  • 1k
Loading