LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-16-2009, 01:46 AM   #1
tianlijian
LQ Newbie
 
Registered: Mar 2007
Posts: 27

Rep: Reputation: 15
a question about bash programming


> a=b echo hi
> echo $a // why this command does not print 'b'?


thank for you opinion!

Last edited by tianlijian; 03-16-2009 at 01:48 AM.
 
Old 03-16-2009, 02:38 AM   #2
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
If you put more than 1 command in the same line - you need a semicolon to separate them:

a=b; echo hi
echo $a; # will print 'b' now

Read the advanced bash scripting guide:
http://tldp.org/LDP/abs/html/
 
Old 03-16-2009, 02:40 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
> a=b echo hi
> echo $a // why this command does not print 'b'?

The a=b parameter assignment effects the environment for the echo command on the first line.

In another post, a user was having a problem opening a password protected pdf file. The poster knew the password, but it wasn't accepted. The solution was to change the local that the pdf reader program used by changing the LC_LOCALE variable on the same line preceding the command.

From 3.7.4 of the bash info manual:
Code:
   The environment for any simple command or function may be augmented
temporarily by prefixing it with parameter assignments, as described in
*Note Shell Parameters::.  These assignment statements affect only the
environment seen by that command.
Try this out:
Code:
function fun1()
{
   echo $a
}

a=10
a=5 fun1()
echo $a
Also look at:
Code:
a=10
( a=5; echo $a )
echo $a
However, try these as well; can you figure out the difference?:
Code:
$ cat test2
echo $a

$ a=100

$ a=3 ./test2
3

$ echo $a
100

# Maybe you weren't expecting the following

$ a=50

$ a=20 echo $a
50

# Here is a trick to use to see shell parameter expansion at work:
a=10
set a=20 echo $a
$ echo -e "$1\t$2\t$3\n"
/bin/bash     a=5     echo    10
As you can see, the $a argument is expanded before the line is run. So it is a constant before bash assigns a=5 to the environment of echo.

One more:
Code:
$ testvar=15

$ testvar=10 env | grep testvar
testvar=10
The env command lists the environment variables.

Last edited by jschiwal; 03-16-2009 at 02:42 AM.
 
Old 03-16-2009, 05:44 AM   #4
tianlijian
LQ Newbie
 
Registered: Mar 2007
Posts: 27

Original Poster
Rep: Reputation: 15
I understand it now. Thank you very much.

Last edited by tianlijian; 03-16-2009 at 06:17 AM.
 
  


Reply


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
Bad: help me bash programming , newbie question CheeSen Programming 2 09-02-2008 09:23 PM
bash programming question - hand over variables to another script ppr:kut Linux - General 5 02-09-2008 10:00 AM
Question about bash shell programming walterbyrd Linux - General 1 11-01-2005 09:42 AM
very simple bash programming question!? Thinking Programming 7 06-01-2005 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:26 PM.

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