Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-24-2005, 11:25 PM   #1
LQ Newbie
Registered: Nov 2003
Location: Ohio
Distribution: SUSE 9.0
Posts: 13

Rep: Reputation: 0
bash shell script expression

What does this expression do?

[ "$1 != "" ] && variable=$1

I understand the test expression return a zero exit status if $1 equals something. Then the result of logical ANDing the exit status with variable is assigned to $1. I do not understand using a zero or nonzero exit status in a logical AND with a variable. Thank you for your help.
Old 04-25-2005, 12:33 AM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

It's a replacement for an if then (kind of). The && means 'only evaluate the second part if the first part is true' (as opposed to & which will evaluate both parts no matter what), so if '$1 == ""', then 'variable=$1' will be executed. If the first part is true, and if the assignment part is successful, then the whole expression will be true.

If, though, '$1 != ""', then the assignment part will not be executed, and the whole expression will be false.

Old 04-25-2005, 12:33 AM   #3
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Most languages use "short circuit" conditions to save computation time when evaluating expressions. If you have some "A and B" expression, and A is false, then B will not be evaluated because the whole expression is going to be false regardless of what B evaulates to.
So in your example the variable assignment on the right will never happen if the expression on the left evaluates false.
Really its just a cryptic shorthand for this
if [ "$1" != "" ]; then variable=$1; fi
You can do similar things with binary OR operator instead of AND, but in this case the right side argument is evaluated only when the left argument is false.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell script moodupani Linux - General 2 09-01-2005 07:14 AM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 06:07 AM
bash shell script naka0naka Linux - Newbie 7 05-28-2004 04:06 PM
bash/shell script mikis Programming 2 10-14-2003 12:39 PM
BASH shell script help ewarmour Programming 8 05-24-2002 08:57 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration