Explain Usage Of The Linux Left Bracket /usr/bin/[ Command

www.lautt‮moc.iru‬
Explain Usage Of The Linux Left Bracket /usr/bin/[ Command

The [ command (also known as the test command) is a built-in shell command that is used to evaluate expressions and perform conditional tests. It is typically used in shell scripts to check the value of a variable or the existence of a file or directory.

The [ command is usually followed by an expression that is surrounded by square brackets ([ and ]) and one or more options or arguments. The expression is evaluated as either true or false, depending on the specified conditions.

Here are some examples of how you can use the [ command:

  • To check if a file exists, you can use the -e option followed by the file's name:
[ -e file_name ]

This will return a true value if the specified file exists and a false value if it does not.

  • To check if a file is a regular file (i.e., not a directory or a special file), you can use the -f option followed by the file's name:
[ -f file_name ]

This will return a true value if the specified file is a regular file and a false value if it is not.

  • To check if a variable has a non-empty value, you can use the -n option followed by the variable's name:
[ -n "$variable" ]

This will return a true value if the specified variable has a non-empty value and a false value if it is empty.

  • To check if two variables have the same value, you can use the = operator:
[ "$variable_1" = "$variable_2" ]

This will return a true value if the two variables have the same value and a false value if they do not.

For more information about the [ command and its available options, you can consult the test man page or use the [ --help command.

Created Time:2017-10-28 20:14:16  Author:lautturi