LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-08-2012, 05:37 PM   #1
FireRaven
Member
 
Registered: Apr 2006
Location: Australia
Distribution: Debian Squeeze
Posts: 135

Rep: Reputation: 18
BASH script question - grouping conditions in brackets?


Hi guys, a quick simple 3 line bash script - the echo's say it all.
Why does the last line echo when the second last echo doesn't?

I would expect my script to exit at line 2 and not print line 3, yet it prints line 3?

Code:
#!/bin/bash
[[ 1 == 1 ]] && (echo 'Should see me!';exit 0;echo 'But you cant see me?')
echo 'Should NOT see me!'
Output:
Code:
user@ubuntu:/tmp$ ./test.sh 
Should see me!
Should NOT see me!
 
Old 03-08-2012, 06:18 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Commands placed inside '()' are run in a subshell, you're only exiting the subshell, not the parent. Try '{}' to execute in the current shell, don't forget a trailing ';'

Code:
#!/bin/bash
[[ 1 == 1 ]] && { echo 'Should see me!';exit 0;echo 'But you cant see me?'; }
echo 'Should NOT see me!'

Last edited by kbp; 03-08-2012 at 06:21 PM. Reason: Add fixed code
 
1 members found this post helpful.
Old 03-08-2012, 06:38 PM   #3
FireRaven
Member
 
Registered: Apr 2006
Location: Australia
Distribution: Debian Squeeze
Posts: 135

Original Poster
Rep: Reputation: 18
Thanks kbp that works.
I didn't know that ran in a subshell, thought they were written with $().
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script "if then" with multiple conditions dohpaz Programming 8 05-24-2012 04:31 PM
[SOLVED] BASH script conditions roud9 Programming 5 02-27-2012 09:21 AM
brackets in bash shell lipun4u Programming 3 06-19-2010 08:24 AM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM
bash - while + until loops grouping? trees Linux - General 2 02-19-2004 02:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:43 AM.

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