Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [] do in bash? [duplicate]

Tags:

bash

Possible Duplicate:
bash: double or single bracket, parentheses, curly braces

Looking at the rc.d cron script in archlinux:

#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions

name=crond
. /etc/conf.d/crond
PID=$(pidof -o %PPID /usr/sbin/crond)

case "$1" in
start)
    stat_busy "Starting $name daemon"
    [[ -z "$PID" ]] && /usr/sbin/crond $CRONDARGS &>/dev/null \
    && { add_daemon $name; stat_done; } \
    || { stat_fail; exit 1; }
    ;;

While I can figure out most of the syntax, what the heck does this do:

 [[ -z "$PID" ]]

I saw that also written as:

 [ -z "$PID" ]

In reference I found that [] is used in if-statements, but I see none here. Any help is much appreciated. Thanks!

like image 704
Andriy Drozdyuk Avatar asked Aug 03 '12 13:08

Andriy Drozdyuk


People also ask

What do square brackets do in bash?

The square brackets are a synonym for the test command. An if statement checks the exit status of a command in order to decide which branch to take. grep -q "$text" is a command, but "$name" = 'Bob' is not--it's just an expression.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does bracket mean in bash?

Parentheses are often used for enclosing conditions and for function parameters. Braces are used for function bodies and objects. And brackets are used for lists or array notation. In Bash, that's often a little different and in many cases, parentheses, braces, and brackets act as commands themselves.

What does $() mean bash?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).


1 Answers

The opening bracket ([) is an alias for the test command which performs all the tests and returns 0 for true or something else for false. The "if" reacts only to the return value of the test command. The closing bracket tells test where the expression ends. The double brackets ([[) are a bash built in and can replace the external call to test.

like image 145
Mithrandir Avatar answered Oct 09 '22 11:10

Mithrandir