ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
unset STRING #make sure we start with a clean slate
echo "wassup" | read STRING # note that this line produces no output
echo $STRING # though it ought to, this line produces no output
I've also tried some variations of read, including those that ignore the newline character. For some reason, "wassup" isn't assigned to $STRING. Since line 2 produces no output, it's safe to assume that "wassup" is being piped to read. And since running line 2 with "read -t 3" returns instantaneously (instead of waiting three seconds), it's safe to assume that read is seeing some data. Anyone know what I'm doing wrong?
The variable is still "lost" if the entire procedure occurs within a shell script instead of being run line by line by the user.
Any idea how to make bash not "lose" my variable? Using ksh exclusively for this purpose seems like a poor solution. (Although I might just switch to ksh altogether...)
Originally posted by kc8tbe The variable is still "lost" if the entire procedure occurs within a shell script instead of being run line by line by the user.
I think you didn't understand it correctly when jlliagre said:
Quote:
This is a bash "feature", read is run by a subshell so your variable is set but lost immediatly.
In case of bash, "read" is not a program, but a builtin command. Then realize that a pipe ('|') connects the output of one process to the input of another. So, to have "read" read from a pipe there is another process needed. Since "read" is built inside bash, another bash-process is started to run "read". The reading of the variable occurs in a different process, which terminates inmediatly after running "read". When process isn't there anymore, all of its variables are lost.
This is no different from the command line or in a single script!
osvaldomarques' soltution is the "standard" way to read the output of a command (either builtin, or a seperate program or process):
Code:
# The classic syntax
STRING=`echo "wassup"`
# newer, bash's syntax (less comaptible, but has some advantages)
STRING=$(echo "wassup")
In this simple case of assigning a string to a variable, it's a "detour" to do this. To assign a string to a variable, you wou'd normally do:
Code:
STRING="wassup"
.
If one these doesn't do what you need for your specific purpose, please explain what it is you are trying to get done by the script.
Thanks everyone! I had completely forgotten about the backtick (`). STRING = `echo "wassup"` does what I want it to.
For the curious, I'm writing a script to update /etc/hosts. Here's an excerpt:
Code:
#!/bin/sh
# Get an ip address for the domain name specified on the commandline.
IPADDR=`host $1 | egrep -o "(([0-9]{3}|[0-9]{2}|[0-9]{1})(\.?)){4}"`
# Do something useful with the $IPADDR (i.e. write it to a file).
# ...
# echo $IPADDR for the world to see,
echo $IPADDR
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.