1847614 Members
2759 Online
110265 Solutions
New Discussion

Re: sh/bash question

 
SOLVED
Go to solution
Alex Lavrov
Regular Advisor

sh/bash question

Hello,
when I'm in sh/bash and I execute variable that do not exists, I don't get any message. Nothing happens:
srv1> $blablabla
srv1> echo $?
0
srv1>

Well, I believe that it's ok, but I would like it to tell me that variable was not set and do not exist (like in csh and tcsh).
Is it possible?

Thanx
6 REPLIES 6
Sridhar Bhaskarla
Honored Contributor
Solution

Re: sh/bash question

Hi,

'set -u' does the trick for you in posix and ksh. Not sure about bash though.

set -u
echo "$VAR"

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Todd McDaniel_1
Honored Contributor

Re: sh/bash question

HPUX posix shell does complain if you execute a $VAR on the command line...

# $VAR
sh: VAR: Parameter not set.

# echo $VAR
sh: VAR: Parameter not set.
Unix, the other white meat.
Jan Zalman
Advisor

Re: sh/bash question

Regardless of the set, ${srv1:?} notation does the job too. Regards.
Time and loyalty cannot be bought.
Steven E. Protter
Exalted Contributor

Re: sh/bash question

I have found variable assignment works exactly the same in bash verus posix and korn.

I've taken A. Clay Stephenson's caljd.sh scripts, changed the shell and gotten completely consistent results.

filevar=steve.file
export filevar

while read -r aa
do
echo $aa
done < $filevar

Will work in all three shells.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jeroen Peereboom
Honored Contributor

Re: sh/bash question

Well,

I think (as stated already by someone else)
'set -u' does the trick.
It means: treats unset variables as an error.

Apparently in your shell this has not been executed, so if you type $blablabla as a command, nothing gets executed. Apparently Todd 's shell has been told to treat unset variables as an error.

In addition I would suggest to add 'set -u' to your own shell scripts. It makes debugging easier (misspelled variables are detected immediately) and may prevent you from erasing a wrong directory.

I could not test Jan's solution...

Greetings,

JP.
Alex Lavrov
Regular Advisor

Re: sh/bash question

*Thanx a lot* for the answers. I'll try it first thing in the morning and assign points to all.

Best regards