Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Elementary question on script variable assignment
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 12-20-2007   #1 (permalink)
Ezo
Just Joined!
 
Join Date: Dec 2007
Posts: 10
Smile Elementary question on script variable assignment

I'm confused why this assignment works:

Code:
echo 'store this' | while read val1
do
echo "$val1"
done
but this doesn't:

Code:
echo 'store this too' | read val2

echo "$val2"
I can assign the value with this:

Code:
val3=$(echo 'store this aswell') > /dev/null

echo "$val3"
but it seems too longwinded.

What's the best method for storing a string from a script statement?

Many thanks.
Ezo is offline   Reply With Quote
Old 12-20-2007   #2 (permalink)
anomie
Linux Guru
 
anomie's Avatar
 
Join Date: Mar 2005
Location: Texas
Posts: 1,699
If all you're doing is assigning a literal string to a variable, how about:

$ VAL1='Store This'

If you need to store the value of some operation, this is pretty standard - e.g.:

$ VAL2=$( hostname )
__________________
FreeBSD Handbook & FAQ // CentOS wiki

Last edited by devils casper; 12-20-2007 at 06:10 PM. Reason: posts merged.
anomie is offline   Reply With Quote
Old 12-20-2007   #3 (permalink)
Ezo
Just Joined!
 
Join Date: Dec 2007
Posts: 10
Righty I'll stick with that method then.

Thanks
Ezo is offline   Reply With Quote
Old 12-22-2007   #4 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 977
Quote:
Originally Posted by Ezo View Post
I'm confused why this assignment works:
Code:
echo 'store this' | while read val1
do
echo "$val1"
done
but this doesn't:
Code:
echo 'store this too' | read val2
echo "$val2"
The reason the second example doesn't work is because when you use a pipe, the command is executed in a subshell, so the parent process can't see the assigned variable. The first example doesn't really work, either, and if you echo the variable after the "done" you'd realise this. Of course, this is the behaviour of bash, both examples work perfectly in a decent shell like ksh.
scm is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupd question about environment variable Lele Linux Programming & Scripting 7 07-25-2007 03:55 PM
a most elementary question pre running mysqld_safe mghere Linux Newbie 0 08-23-2006 03:43 AM
Asking good questions (2) jasonlambert Linux Newbie 5 12-13-2004 09:22 PM
noob shell script question Dillinger Linux Programming & Scripting 4 02-13-2004 12:13 AM
Shell script noobie question? Mzzbizzsnatch Linux Programming & Scripting 4 09-10-2003 09:02 PM




All times are GMT. The time now is 09:55 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0