LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-23-2021, 07:40 AM   #1
bryn1u
LQ Newbie
 
Registered: Feb 2014
Posts: 9

Rep: Reputation: Disabled
cmd bash "cat" behaves differently when is added to variable


Hey guys,

Im trying to use cat which is assigned to variable. But the output of "cat" is differently then from command line, why ?
Code:
root@Proton:[~]:#> cat text
dupufufr3
frerfr3
3r
edziopedzio
frwnfru
fjwlfrew
fklwkfw
fkwf

root@Proton:[~]:#> variable="$(cat text)"
root@Proton:[~]:#> echo $variable
dupufufr3 frerfr3 3r edziopedzio frwnfru fjwlfrew fklwkfw fkwf
Why this is interpreted differently ? How can i get the same output as from command line ?

Thank you,
 
Old 07-23-2021, 10:39 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Because the shell further expands unquoted $variables in command arguments (and in for lists).
Code:
variable=$(cat text) # assignment: no further expansions aftet $-substitution
echo "$variable" # quotes permit $-substitution but no further expansions
The further expansions are
1. field splitting (using $IFS, InputFieldSeparator)
2. filename generation (e.g. if $variable has * wildcards).
 
1 members found this post helpful.
Old 07-23-2021, 01:30 PM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I'll add that cat behaves the same. It's echo that makes the difference.

From the GNU Bash manual:
Quote:
echo
Code:
echo [-neE] [arg …]
Output the args, separated by spaces, terminated with a newline.
 
1 members found this post helpful.
Old 07-24-2021, 04:31 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
also "$variable" might show different results to $variable.
 
  


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
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 08:08 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] "su -l user -c command" behaves differently to other Linux/UNIX GazL Slackware 6 06-30-2010 09:51 AM
applet on linux server behaves differently naijaguy Programming 3 01-07-2005 02:09 PM
shell behaves differently in rl5 and rl3 Warmduvet Linux - General 22 09-22-2004 05:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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