LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-12-2014, 06:00 PM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Rep: Reputation: Disabled
Using cat to create a file which contain variables


I thought I would be smart, and instead of using vim to create a file, I would use the following:
Code:
cat > /etc/yum.repos.d/PUIAS_6_computational.repo <<EoT
[PUIAS_6_computational]
name=PUIAS computational Base $releasever - $basearch
mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist
#baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch
enabled=1
priority=90
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
EoT
But when I looked at the file I created, I saw the following:
Code:
[root@desktop ~]# cat /etc/yum.repos.d/PUIAS_6_computational.repo
[PUIAS_6_computational]
name=PUIAS computational Base  -
mirrorlist=http://puias.math.ias.edu/data/puias/computational///mirrorlist
#baseurl=http://puias.math.ias.edu/data/puias/computational//
enabled=1
priority=90
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
[root@desktop ~]#
So, I learned something. cat interprets anything that starts with a dollar sign ($) as a variable, and attempts to resolve it. Just on a hunch, I tried escaping the dollar signs using a backslash (\), and found it did the trick.

Two questions. Did I solve this issue correctly? Is this common to many Linux commands or just cat?

Thanks
 
Old 04-12-2014, 06:18 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
I think you did it rite. you can also try surrounding your string in single quotes.
 
Old 04-12-2014, 06:22 PM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Is this just a cat thing, or is it a typical Linux design pattern?
 
Old 04-12-2014, 06:23 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
I think it affects most text manipulation programs like grep, sed, awk, cut, ...
 
Old 04-12-2014, 06:33 PM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks schneidz, Got to love consistency!
 
Old 04-12-2014, 10:11 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
See my bash quotes link in your other thread
 
Old 04-13-2014, 04:03 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
cat is not expanding the variable names, that would be bash. As suggested, when using a here document, if you quote the limit string at the start and the variables will not be interpreted:
Code:
cat > blah <<'EOF'
 
Old 04-13-2014, 06:10 AM   #8
mddnix
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch, Ubuntu
Posts: 498

Rep: Reputation: 137Reputation: 137
Just in case you need... here is a 2 page examples on here-document.

All You Wanted To Know About HERE Documents in Linux
 
Old 04-13-2014, 07:31 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by NotionCommotion View Post
I thought I would be smart, and instead of using vim to create a file, I would use the following:
Code:
cat > /etc/yum.repos.d/PUIAS_6_computational.repo <<EoT
[PUIAS_6_computational]
name=PUIAS computational Base $releasever - $basearch
mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist
#baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch
enabled=1
priority=90
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
EoT
But when I looked at the file I created, I saw the following:
Code:
[root@desktop ~]# cat /etc/yum.repos.d/PUIAS_6_computational.repo
[PUIAS_6_computational]
name=PUIAS computational Base  -
mirrorlist=http://puias.math.ias.edu/data/puias/computational///mirrorlist
#baseurl=http://puias.math.ias.edu/data/puias/computational//
enabled=1
priority=90
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
[root@desktop ~]#
So, I learned something. cat interprets anything that starts with a dollar sign ($) as a variable, and attempts to resolve it. Just on a hunch, I tried escaping the dollar signs using a backslash (\), and found it did the trick.
Nope. cat doesn't interpret anything - it just copies. What does the interpretation is the shell because you used a "hereis" redirection.
Quote:
Two questions. Did I solve this issue correctly? Is this common to many Linux commands or just cat?

Thanks
The command you show is correct... the problem is that the environment variables don't seem to be defined.
 
Old 04-13-2014, 08:23 AM   #10
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
What does the interpretation is the shell because you used a "hereis" redirection.
I don't understand. Please elaborate.

Quote:
Originally Posted by jpollard View Post
the problem is that the environment variables don't seem to be defined.
I didn't think they were environment variables by yum internal variables. Reference http://www.linuxquestions.org/questi...8/#post5151390
 
Old 04-13-2014, 10:11 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
The shell is interpreting the extended command line

Code:
cat > /etc/yum.repos.d/PUIAS_6_computational.repo <<EoT
[PUIAS_6_computational]
name=PUIAS computational Base $releasever - $basearch
mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist
#baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch
enabled=1
priority=90
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
EoT
cat doesn't see anything until the shell finishes parsing the command line, and that parsing includes translating any environment variables it sees within the command line (unless prevented by appropriate quoting/escaping of the "$" characters). Once the command "line" has been parsed, the process (cat) is started, and the redirected (and interpreted) input is sent to stdin. As such, the environment variables referenced ($releasever and $basearch) will have been replaced by their values. Since there are no values, the variable names get replaced by nothing.
 
Old 04-13-2014, 11:20 AM   #12
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks jpollard, makes sense!

Thanks mddesai, Sorry, I accidentally missed your post before responding. Great article.

Thanks Grail, Sorry, I accidentally missed your post before responding. Great advice about using 'EOF' instead of EOF. I am glad it works, but why? I saw it documented in mddesai's recommended article, but where else is this documented? EDIT. Is it good practice to always put single quotes around the deliminator when you know you don't want anything parsed?

Last edited by NotionCommotion; 04-13-2014 at 11:24 AM. Reason: Add part about good practice
 
Old 04-13-2014, 12:10 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I believe all references to here documents will advise on the difference between quoting or not. Here is another.

As for good practice, if you do not want the variables to be interpreted by the shell then it is the only choice when using a here document.
 
  


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
Bash to find all placeholder variables in a file and replace will real variables spadez Programming 6 11-26-2013 01:24 PM
[SOLVED] Why does cat > sample.txt create a new file ? Grtyop Linux - Newbie 9 05-31-2013 01:40 PM
LXer: Using Cat Command in Linux To Create,Display,Append and Concatenate files LXer Syndicated Linux News 0 08-01-2011 05:50 PM
.sh script to create file, with variables occamsrazor Programming 2 04-28-2011 12:19 AM
how to stop parsing shell variables in bash script inside cat area? jackandking Linux - Newbie 2 03-10-2009 07:44 AM


All times are GMT -5. The time now is 10:06 PM.

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