LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-10-2018, 03:46 PM   #1
gnuoob
LQ Newbie
 
Registered: Sep 2003
Posts: 19

Rep: Reputation: 0
Valid Echo command?


Hello,

I have a tinkering question with the BASH terminal and wondered if the following command is workable or I'm just doing something outside of the box of reality.

$: echo "New_Line#3" >> file*.txt

In said folder I have two text files named 'file1.txt' and 'file2.txt'

I can't get it to work but is it even possible?



Thank you in advance
 
Old 06-10-2018, 04:13 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
That command will append the string
Quote:
New_Line#3
into a file named
Quote:
file*.txt
If run a second time, it will append another line to the same file.

You'd need to use tee to direct the output of echo to two different files, and you'd need to name them both.
Code:
echo "New_Line#3" | tee file1.txt >> file2.txt
See man tee

I'd be more inclined to do
Code:
echo "New_Line#3" >> file1.txt; cp file1.txt file2.txt
 
Old 06-10-2018, 04:38 PM   #3
gnuoob
LQ Newbie
 
Registered: Sep 2003
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you for the reply.

I was thinking I could use the wild card to put the same new test into a series of files but that is not the case.

In my scenario I wasn't looking to copy 1 to 2 but was instead just trying to add New_Line#3 to the end of a multiple of files.
 
Old 06-10-2018, 04:41 PM   #4
gnuoob
LQ Newbie
 
Registered: Sep 2003
Posts: 19

Original Poster
Rep: Reputation: 0
my other attempts of using

$: echo "New_Line#3" >> file[1,2].txt

&

$: echo "New_Line#3" >> file?.txt

were just as unfruitful
 
Old 06-10-2018, 05:02 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Use a loop
Code:
for f in file*.txt; do echo "New_Line#3" >> $f; done
 
Old 06-11-2018, 12:09 AM   #6
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
Quote:
Originally Posted by gnuoob View Post
$: echo "New_Line#3" >> file[1,2].txt

&

$: echo "New_Line#3" >> file?.txt

were just as unfruitful
all these wildcards work, but the expansion doesn't do what you want it to do:
Code:
touch file{1,2} # works as expected because touch can take multiple filenames
echo file{1,2}
echo file*
echo file?
each time you get
Code:
file1 file2
as output.

so now try
Code:
echo "New_Line#3" >> file1 file2
it appends only to file1, because the >> operator doesn't know that there's another file after the first one.
 
Old 06-14-2018, 03:40 AM   #7
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 510

Rep: Reputation: 58
Quote:
Originally Posted by keefaz View Post
Use a loop
Code:
for f in file*.txt; do echo "New_Line#3" >> $f; done
that's what I do, what I've always seen the "old Unix hackers" do. that probably means it's the designed method for doing it.


you could write a C program to do what your thinking or even re-compile bash hacked to do that: but you couldn't get it documented in millions of Unix manpages or distributed to others. only you would be using it.

if you wrote a program to build software and included it: it wouldn't work on anyone else's system but your own - you could not share your scripts

Last edited by X-LFS-2010; 06-14-2018 at 03:42 AM.
 
Old 06-14-2018, 04:01 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
you cannot redirect anything into more than one file, redirection is unable to handle it.

but actually tee can do that for you:
Code:
echo "New_Line#3" | tee -a file*.txt >/dev/null
(not tested)
 
1 members found this post helpful.
Old 06-14-2018, 09:11 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by pan64 View Post
you cannot redirect anything into more than one file, redirection is unable to handle it.

but actually tee can do that for you:
Code:
echo "New_Line#3" | tee -a file*.txt >/dev/null
(not tested)
Just tested, works nice
 
  


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
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
Echo Command Dougj75 Linux - Newbie 2 02-28-2012 07:09 PM
echo command vinothaa Linux - Newbie 5 06-05-2010 08:15 AM
echo command mohit_parihar Linux - General 3 01-12-2010 06:54 AM
echo command s_hcl Linux - General 1 09-06-2006 05:27 AM

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

All times are GMT -5. The time now is 10:36 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