helpermethod
helpermethod

Reputation: 62185

What's a null string in bash

I've just found this snippet in the bash manual:

A variable may be assigned to by a statement of the form

  name=[value]

If value is not given, the variable is assigned the null string.

What exactly is meant by null string? Is e.g.

local empty

equivalent to

local empty=""

?

Upvotes: 4

Views: 2877

Answers (1)

dogbane
dogbane

Reputation: 274632

A "null string" is one which has zero length. In your example, both are the same.

A simple test:

#!/bin/bash
go(){
   local empty
   local empty2=""
   [[ -z $empty ]] && echo "empty is null"
   [[ -z $empty2 ]] && echo "empty2 is null"
   [[ $empty == $empty2 ]] && echo "They are the same"
}

go

prints:

empty is null
empty2 is null
They are the same

Upvotes: 9

Related Questions