An Example Of An Empty String?


Recommended Posts

I am tring to understand what a "zero lenght" or non zero length string" string would be. That seems to be a string with no characters, but then it would be nothing

example

[ -n $TEST ]
[ -z $VAR ]

Edited by shanenin
Link to post
Share on other sites

is that meant to check if that is a set variable?

In my mind these should be outputting 0 for the first one but 1 for the second one.

shane@mainbox shane $ gg=""; [ -z $gg ]; echo $?
0
shane@mainbox shane $ gg=""; [ -n $gg ]; echo $?
0

Edited by shanenin
Link to post
Share on other sites
is that meant to check if that is a set variable?

Among other things.

In my mind these should be outputting 0 for the first one but 1 for the  second one.

After variable substitution

$ gg=""; [ -n $gg ]; echo $?

becomes

$ [ -n ]; echo $?

and 'test' is defined such that if there's only one argument the exit status is 0 if that argument is non-null and 1 otherwise. -n is treated as a regular argument rather than operator in this case. Not sure if this is documented but it's POSIX behavior.

It will work if you quote '$gg' in the test to ensure that you get at least the empty string

$ gg=""; [ -z "$gg" ]; echo $?
0
$ gg=""; [ -n "$gg" ]; echo $?
1

Edited by jcl
Link to post
Share on other sites
is that meant to check if that is a set variable?
Among other things.

sorry you already answered that(I missed it)

I am sure you already knew these are equal expressions

test -z $var

[ -z $var ]

Edited by shanenin
Link to post
Share on other sites
is that meant to check if that is a set variable?
Among other things.

sorry you already answered that(I missed it)[/q]

I think I added that in an edit while you were posting. One of these days I'll learn to write my posts all at once instead of doings edits for half an hour :D

Link to post
Share on other sites
Among other things.

I found one of those other things.

#!/bin/bash
RESPONSE=
while [ -z "$RESPONSE" ];
do
  echo "Enter the name of the directory where your files are located:\c "
  read RESPONSE
  if [ ! -d "$RESPONSE" ]; then
     echo "ERROR: please enter a directory pathname."
     RESPONSE=
  fi
done

according to the book, my output should be

Enter the name of the directory where your files are located:

but I am getting

Enter the name of the directory where your files are located:\c

I thought that the "\" worked as quoting. So that would disable a special character? the letter "c" does not seem to be a special character

Link to post
Share on other sites

Unix uses backslash for two purposes: to disable special interpretation of some characters (quotation marks, spaces, etc) and to enable special interpretation of other characters. For example, '\n' is often interpreted as a newline character and \t as a (horizontal) tab.

In your example, '\c' is supposed to prevent echo from printing a trailing newline. However, that's a non-standard feature and support varies from shell to shell and platform to platform. bash and GNU echo will enable backslash sequence processing if you pass the '-e' option to echo.

echo -e "Enter the name of the directory where your files are located:\c "

Officially if you need formatted output you're supposed to use the 'printf' utility. printf was created because "irreconcilable differences in the various versions of echo extant" made it impossible to standardize any formatting options for echo.

Edited by jcl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...