LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2017, 10:13 AM   #1
Neville Hillyer
Member
 
Registered: Jul 2015
Posts: 57

Rep: Reputation: Disabled
Bash - file content to string can be dangerous


In Bash I use y=$(<tmp) to put the contents of a file into a string but I was caught out today with a JavaScript file starting with /*

Here is a test case you can try:

echo '/*' > tmp; y=$(<tmp); echo $y

This could be dangerous. How should it be overcome especially for files with unknown content?
 
Old 04-11-2017, 10:17 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Add quotes around variables when you used them.

echo '/*' > tmp; y=$(<tmp); echo "$y"
 
1 members found this post helpful.
Old 04-11-2017, 10:25 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,913

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
just a comment: echo '/*' > tmp; y=$(<tmp); worked perfectly, so the variable y contained what you wanted/expected, only the last part was "strange": echo $y does not (only) print the value of y but first it was evaluated by bash. If you want to avoid that use " as it was explained in post #2
 
Old 04-11-2017, 10:40 PM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
for that, and other reasons, it's good to make a habit out of placing quotes and braces around variables. Makes your code look more cosistent.

See the difference in code:
Code:
echo '/*' > /tmp/testfile
y="$(</tmp/testfile)"
echo "${y}"
echo "-----"
echo "$y"
echo "-----"
echo '${y}'
echo "-----"
echo $y
notice the difference what each type of quotes does btw; at times that can be handy.
 
Old 04-12-2017, 04:39 AM   #5
Neville Hillyer
Member
 
Registered: Jul 2015
Posts: 57

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
Add quotes around variables when you used them.

echo '/*' > tmp; y=$(<tmp); echo "$y"
How silly of me - thanks - "$y" works.

I thought I had tried this but clearly something went wrong. It was a small part in a larger script but clearly I did not adequately test my own simplified demonstration script.
 
Old 04-12-2017, 10:21 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
NEVER read a file using a for loop that has unknown content. Use a while loop.
 
Old 04-12-2017, 10:24 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
for more on that look here if you think you are ready for "Advanced Bash-Scripting"
5.1. Quoting Variables

Last edited by BW-userx; 04-12-2017 at 10:26 AM.
 
  


Reply



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
re:bash - how would I test for a string from a file and do something based on $string slacker_ Programming 2 06-09-2014 03:35 AM
[SOLVED] Bash: back-referencing / shuffle string content using internals druuna Programming 2 11-27-2013 11:48 AM
[SOLVED] Bash- file names with associated file content towardstheedge Programming 4 12-06-2012 12:59 PM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 01:41 PM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12: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
Open Source Consulting | Domain Registration