Skip to main content
added 1 character in body
Source Link
zuazo
  • 3.1k
  • 21
  • 24

[ is a shell builtin command in bash and in zsh:

$ type [
[ is a shell builtin

From the Shell Builtin Commands documentation:

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.

The official documentation ($ help test) only allows to use =:

STRING1 = STRING2

True if the strings are equal.

So, the correct expression would be:

$ [ "a" = "a" ] && echo yes
yes

What happens is that bash is a bit less strict. Supporting the == operator with [ seems to be a bash extension and it is no recommended to use it:

string1 == string2

string1 = string2

True if the strings are equal. When used with the [[ command, this performs pattern matching as described above (see Conditional Constructs).

‘=’ should be used with the test command for POSIX conformance.

If you want to use ==, you should use the [[ keyword:

$ [[ "a" == "a" ]] && echo yes
yes

Keep in mind that [[ is less portable (is not POSIX). But both bash and zsh support it.

[ is a shell builtin command in bash and in zsh:

$ type [
[ is a shell builtin

From the Shell Builtin Commands documentation:

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.

The official documentation ($ help test) only allows to use =:

STRING1 = STRING2

True if the strings are equal.

So, the correct expression would be:

$ [ "a" = "a" ] && echo yes
yes

What happens is that bash is a bit less strict. Supporting the == operator with [ seems to be a bash extension and it is no recommended to use it:

string1 == string2

string1 = string2

True if the strings are equal. When used with the [[ command, this performs pattern matching as described above (see Conditional Constructs).

‘=’ should be used with the test command for POSIX conformance.

If you want to use == you should use the [[ keyword:

$ [[ "a" == "a" ]] && echo yes
yes

Keep in mind that [[ is less portable (is not POSIX). But both bash and zsh support it.

[ is a shell builtin command in bash and in zsh:

$ type [
[ is a shell builtin

From the Shell Builtin Commands documentation:

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.

The official documentation ($ help test) only allows to use =:

STRING1 = STRING2

True if the strings are equal.

So, the correct expression would be:

$ [ "a" = "a" ] && echo yes
yes

What happens is that bash is a bit less strict. Supporting the == operator with [ seems to be a bash extension and it is no recommended to use it:

string1 == string2

string1 = string2

True if the strings are equal. When used with the [[ command, this performs pattern matching as described above (see Conditional Constructs).

‘=’ should be used with the test command for POSIX conformance.

If you want to use ==, you should use the [[ keyword:

$ [[ "a" == "a" ]] && echo yes
yes

Keep in mind that [[ is less portable (is not POSIX). But both bash and zsh support it.

Source Link
zuazo
  • 3.1k
  • 21
  • 24

[ is a shell builtin command in bash and in zsh:

$ type [
[ is a shell builtin

From the Shell Builtin Commands documentation:

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.

The official documentation ($ help test) only allows to use =:

STRING1 = STRING2

True if the strings are equal.

So, the correct expression would be:

$ [ "a" = "a" ] && echo yes
yes

What happens is that bash is a bit less strict. Supporting the == operator with [ seems to be a bash extension and it is no recommended to use it:

string1 == string2

string1 = string2

True if the strings are equal. When used with the [[ command, this performs pattern matching as described above (see Conditional Constructs).

‘=’ should be used with the test command for POSIX conformance.

If you want to use == you should use the [[ keyword:

$ [[ "a" == "a" ]] && echo yes
yes

Keep in mind that [[ is less portable (is not POSIX). But both bash and zsh support it.