LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using cat to create a file which contain variables (https://www.linuxquestions.org/questions/linux-newbie-8/using-cat-to-create-a-file-which-contain-variables-4175501476/)

NotionCommotion 04-12-2014 05:00 PM

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

schneidz 04-12-2014 05:18 PM

I think you did it rite. you can also try surrounding your string in single quotes.

NotionCommotion 04-12-2014 05:22 PM

Is this just a cat thing, or is it a typical Linux design pattern?

schneidz 04-12-2014 05:23 PM

I think it affects most text manipulation programs like grep, sed, awk, cut, ...

NotionCommotion 04-12-2014 05:33 PM

Thanks schneidz, Got to love consistency!

chrism01 04-12-2014 09:11 PM

See my bash quotes link in your other thread :)

grail 04-13-2014 03:03 AM

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'

Madhu Desai 04-13-2014 05:10 AM

Just in case you need... here is a 2 page examples on here-document.

All You Wanted To Know About HERE Documents in Linux

jpollard 04-13-2014 06:31 AM

Quote:

Originally Posted by NotionCommotion (Post 5151403)
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.

NotionCommotion 04-13-2014 07:23 AM

Quote:

Originally Posted by jpollard (Post 5151661)
What does the interpretation is the shell because you used a "hereis" redirection.

I don't understand. Please elaborate.

Quote:

Originally Posted by jpollard (Post 5151661)
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

jpollard 04-13-2014 09:11 AM

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.

NotionCommotion 04-13-2014 10:20 AM

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?

grail 04-13-2014 11:10 AM

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.


All times are GMT -5. The time now is 10:39 AM.