LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-18-2012, 01:27 AM   #1
Xplorer4x4
Member
 
Registered: Mar 2012
Location: Evansville,IN
Distribution: Chakra
Posts: 56

Rep: Reputation: Disabled
Bash Script To Concatenate Environmental Variables


I have written a bash script and everything works great except for trying to concatenate some variables in to .bashrc and other system files.

I have 4 instances of edits but here i the edit for bashrc:
Code:
cat >> ~/.bashrc <<EOF
export DEVKITXENON="/usr/local/xenon" 
export PATH="$PATH:$DEVKITXENON/bin:$DEVKITXENON/usr/bin" 
EOF
This does actually edit .bashrc, it just does not actually pass the variables in to the file properly. Here is what it outputs:
Code:
export DEVKITXENON="/usr/local/xenon"
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin:/usr/bin"

Last edited by Xplorer4x4; 03-18-2012 at 01:28 AM.
 
Old 03-18-2012, 01:35 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
You have 2 issues here:

1. The variable DEVKITXENON has yet to be set at point of execution, hence when the here document is processed it is expanding the variable to nothing

2. If you did not want the variable to expand then the PATH variable will also not be expanded

May I also ask, do you want this variable available to the shell or is it merely to be used to fill in the data for the PATH variable?

Depending on how you answer the above will change the solution.
 
Old 03-18-2012, 01:47 AM   #3
Xplorer4x4
Member
 
Registered: Mar 2012
Location: Evansville,IN
Distribution: Chakra
Posts: 56

Original Poster
Rep: Reputation: Disabled
grail, thanks for your response, but keep in mind this is the newbie forum..

1. Assuming I comprehend this correctly, are you saying that the system does not know what DEVKITXENON is because I have not installed a corresponding package for it? If so, that is not the case as I have installed the corresponding package already. If I manually edited .bashrc the system would have no problem comprehending this.

2. ?

I think my original statement was pretty self explanatory. With in a bash scipt, I am trying to use cat to append the two export lines in .bashrc. Where did I go wrong?
 
Old 03-18-2012, 04:22 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Yes I fully understand that this is a newbie forum and I was trying to tell you where you went wrong

Number 1 has nothing to do with what is installed but rather how and when the variable is being expanded, ie. the name of the variable at this point is irrelevant.
In the current here document you have used, all variables will be expanded by the shell, bash, prior to the rest of the process being run.
Hence you type:
Code:
cat >> ~/.bashrc <<EOF
export DEVKITXENON="/usr/local/xenon" 
export PATH="$PATH:$DEVKITXENON/bin:$DEVKITXENON/usr/bin" 
EOF
And before executing the cat / here document, the shell does:
Code:
cat >> ~/.bashrc <<EOF
export DEVKITXENON="/usr/local/xenon" 
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin:/usr/bin" 
EOF
So as the shell currently has no idea what DEVKITXENON is to be set to it is presumed to be not currently set and hence null.

My question surrounding whether or not DEVKITXENON needs to be known to the shell at login was regarding whether or not other applications will look for this to know the value, eg. like JAVA_HOME
can be set and recalled by java applications. If it does not need to be known except to set the PATH variable, then I would set the variable prior to running the here document
and forgo the first line:
Code:
DEVKITXENON="/usr/local/xenon"
cat >> ~/.bashrc <<EOF
export PATH="$PATH:$DEVKITXENON/bin:$DEVKITXENON/usr/bin"
Or if you wish you could also put it into the .bashrc file without the export, but then this brings us to option 2.
This option allows you to deliver the variables as is into your .bashrc and choose either to export or not based on the above information.

Assuming yes to the export:
Code:
cat >> ~/.bashrc <<"EOF"
export DEVKITXENON="/usr/local/xenon" 
export PATH="$PATH:$DEVKITXENON/bin:$DEVKITXENON/usr/bin" 
EOF
By placing the quotes around the here document ending you instruct it not to let the shell evaluate the variables and deliver them as written.

Hope this was clearer
 
1 members found this post helpful.
Old 03-22-2012, 12:27 AM   #5
Xplorer4x4
Member
 
Registered: Mar 2012
Location: Evansville,IN
Distribution: Chakra
Posts: 56

Original Poster
Rep: Reputation: Disabled
Thanks Grail, from what I went with the second method and it seems to work great! Thanks again!
 
Old 03-22-2012, 01:44 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
No problem Please mark as SOLVED if you have a solution
 
Old 03-22-2012, 03:19 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,516

Rep: Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436
Quote:
Originally Posted by Xplorer4x4 View Post
I have written a bash script and everything works great except for trying to concatenate some variables in to .bashrc and other system files.

I have 4 instances of edits but here i the edit for bashrc:
Code:
cat >> ~/.bashrc <<EOF
export DEVKITXENON="/usr/local/xenon" 
export PATH="$PATH:$DEVKITXENON/bin:$DEVKITXENON/usr/bin" 
EOF
I think you simply missed a few backslashes, the second line should look like this:
Code:
export PATH="$PATH:\$DEVKITXENON/bin:\$DEVKITXENON/usr/bin"
 
  


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
Environmental Variables in BASH Source Code BonzoDog7827 Linux - Software 1 03-17-2011 01:26 PM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
Bash script: concatenate variables with period character the182guy Linux - Software 4 12-28-2009 02:41 PM
sed, indentation, bash subscripts, and exporting environmental variables JazzItSelf Programming 5 12-05-2008 06:19 PM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 05:14 PM


All times are GMT -5. The time now is 11:15 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration