Shell scripting question - joining variables or variable + text
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.
Shell scripting question - joining variables or variable + text
Hello,
I've done some searching, but I'm not quite sure how to word this and I am not getting any results. So, I apologize if this has been asked, but I am at a loss for the correct wording.
Anyway I want to try something like this where...
1) you set $var1 to be "cat" (via the read command)
2) you would like to make a file based on the variable's name ex: /home/cat.log
Back in the days when I was a windows user and was on mIRC, you would use mIRC's flavor of scripting like so:
/home/ $+ $var1 $+ .log
and that would translate into /home/cat.log
Is there such a way to do this in the shell? Thank you very much ahead of time!
wow... that worked. I'm somewhat surprised. Is it because you set another variable containing that one? I tried that just plain-text (not setting a second variable) and didn't get results...
As weibullguy shows, there's generally no need to do anything special with bash variables. Simply put them wherever you want the value to appear in the output.
Check out the Advanced Bash Scripting guide chapter on variables. Lots of good info and examples there. I also recommend the next chapter on quoting, which can have a big effect on the output.
wow... that worked. I'm somewhat surprised. Is it because you set another variable containing that one? I tried that just plain-text (not setting a second variable) and didn't get results...
No, it doesn't have anything to do with the second variable. I'm sure he just used it as an example of how to use the output. You can even use variables directly from the command line.
As David the H. stated, it has nothing to do with the second variable. I showed that simply because I presumed you wanted to do something more exciting than echo /home/$var.log. I think it is better to use a variable for this purpose rather than risk a typo every time you use /home/$var.lpg (see I did it right there).
I also second David the H.'s recommended reading. Bookmark it if you plan to do much BASH scripting! Don't get scared about the Advanced title either.
Thanks guys. Yeah that has definitely helped -- I've created two shell scripts to use openssl to generate a CA certificate + key and a user cert/key/pkcs12 set. Just simplifying the process as more of an educational endeavor.
My latest project has been a shell script + cron setup to reapply chmod/chown on several of the shares on the network server because they often get changed for various reasons.
Quote:
Originally Posted by weibullguy
I presumed you wanted to do something more exciting than echo /home/$var.log. I think it is better to use a variable for this purpose rather than risk a typo every time you use /home/$var.lpg
Haha, yeah, I wanted something more exciting than that -- you presumed correctly. That actually helped me a lot, now that I know I can make variables within variables without much trouble (as opposed to some programming languages).
Thank you again, I just had it stuck in my mind that I needed something to join the variables (old dog, new tricks... hehe)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.