[SOLVED] import variable from file into current bash session
Linux - GeneralThis 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
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.
import variable from file into current bash session
Hi,
Is there a way I could run a line which assigns a variable in a script, so that I could use it within the current bash session? In other words, I would like to use the source command only for a certain line in the file. How can I do that?
Right, my problem was that I kept trying to execute the line itself, the line which assigns the variable. So what I'm basically doing is var=$(var=...whatever) or some other var name, but that's irrelevant. Thanks.
If I try var=$(awk 'NR==3 { print }'), I'd expect the variable to be assigned the output of the awk command, but that's not the case. When I echo $var, it says:
Which is normal, of course, because the output of that line is another command in itself. If I try var=$($(awk...)), then I get var=$(ip: command not found
So it doesn't seem to be working after all.
returns an IP address, or nothing. so my_ip will be set to that IP (of your eth0).
when you set mp_ip, eval $line has no any meaning, because the variable line was not set at all.
by the way:
Sorry, I posted in a hurry and wasn't paying attention to some basic things.
You are obviously correct.
And yes, I agree with the fact that your solution is more elegant, especially because you're using only one tool instead of three.
I do have a question, though. How does the * work within the [ /]* regex? If I don't include it, then, in order to get the IP, I have to print $6. If I do, then, ofc, it works as you said it works.
Does it mean any number (including 0) of spaces or slashes? And the reason for it is that the indentation has several spaces? Is there any difference in this case between * and +? I know + means 1 or infinite, but in this case with * I'm guessing you couldn't have zero spaces or slashes as a delimiter. So are they equivalent when it comes to awk delimiters?
Another question:
Is inet.*eth0 a greedy regex? If "eth0" showed up twice, then it would match the string up to the last occurrence, right?
about [ /]*: you are right, + can be used too, zero is meaningless in this case, because -F specifies a delimiter, which cannot be empty (in this case, but there are cases where it is accepted).
with * and/or + subsequent spaces will count as one single delimiter, without that you need to use $6 - as you have already find this.
about inet.*eth0: in this case it is used to replace grep inet | grep eth0, so we need both (inet.*eth0 is order dependent, grep is not). Yes, it is greedy, but it is not really important here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.