for your eyes only
Knowledge Hurts – a blog of thoughts, things and stuff.

I have a nice s…

I have a nice script and want to read its return value in a “bash -c” statement. Here is my script:

#!/bin/bash 
exit 100 

and this is what happens when I execute it:

bash exit100.sh; echo $? 
100 

Nice! It shows me the return value! But wait – what if I use bash -c to run these 2 commands in a subshell? 

bash -c "bash exit100.sh; echo $?" 
0

WTF? It shows me 0 as return value? That can’t be! Well – yes it can. And here is the way to do it right:

bash -c 'bash exit100.sh; echo $?' 
100

Using single quotes (‘) instead of double quotes (“), I get what I want. I execute a subshell, run a script inside it and can read the return value from the $? variable! IT’S MAGIC!!!

Advertisements

No Responses to “I have a nice s…”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: