LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 01-31-2013, 05:56 AM   #1
RandyTech
Member
 
Registered: Oct 2010
Posts: 47

Rep: Reputation: 1
bash: echo followed by command on the same line ???


This is probably a dumb question considering the nature of the 'echo' command...

Code:
echo "this is a test" >/tmp/testfile ; ls -l /tmp/
In a bash script, trying to 'echo' to a file, and on the same line, get the 'ls' command to return the results of that file change. Is that possible?

Please consider this a generic question regarding the 'echo' command itself. I only use the 'ls' command to illustrate the objective --> a 2nd command on the same line following the 'echo' command.

Thanks in advance for specifics on the nature of 'echo'
 
Old 01-31-2013, 06:04 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I'm not sure if you're asking about the output or about the command line. In the first case, if you want the result of the second command inlined with the output of echo, just try the -n option. This makes echo not print a newline at the end.
 
Old 01-31-2013, 07:34 AM   #3
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
My question back would be, what is your example not doing that you would like it to do?

As it stands I see no issue with what you have presented.
 
Old 01-31-2013, 10:26 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Both are different command, and not related. It's like:-
Code:
~$ echo "this is a test" >/tmp/testfile   # Pushing echo message into a file
~$ ls -l /tmp/    # Listing content of /tmp/ directory
Can you explain little more?
 
Old 01-31-2013, 11:43 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Whether the two commands are separated by ";" or by a newline makes absolutely no difference. They are still entirely separate commands.
 
Old 01-31-2013, 12:09 PM   #6
vipin310379
LQ Newbie
 
Registered: Jan 2013
Location: Moga, Punjab
Distribution: Redhat
Posts: 19

Rep: Reputation: Disabled
yes you can run it on command line as well as in script

you can run this on CLI as well as in bash script.

echo "this is a test" >/tmp/testfile ; ls -l /tmp/
 
Old 01-31-2013, 05:32 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Multiple commands are combined with list operators. From the bash man page:

Quote:
Lists
A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or <newline>.

Of these list operators, && and || have equal precedence, followed by ; and &, which have equal precedence.

A sequence of one or more newlines may appear in a list instead of a semicolon to delimit commands.

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

AND and OR lists are sequences of one of more pipelines separated by the && and || control operators, respectively. AND and OR lists are executed with left associativity. An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.
Depending on exactly what you want to do you might also be able to use command substitution to creatively embed one command inside another in some way.
 
Old 01-31-2013, 05:39 PM   #8
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
why didn't you just try it? I did, it worked like a charm
 
Old 01-31-2013, 06:06 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by impert View Post
why didn't you just try it? I did, it worked like a charm
Sometimes the results are indeterminate, and you really need to know what is assured. For example:
Code:
echo "this is a test" >/tmp/testfile & ls -l /tmp/
Now the first command is being run in the background, and there is no assurance which command will run first. Indeed, since echo is a shell builtin command, the results could be entirely different from what you might see with an external command, but in neither case is the result guaranteed one way or the other.
 
Old 02-01-2013, 06:38 AM   #10
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
Code:
echo "this is a test" >/tmp/testfile & ls -l /tmp/
The OP had:
Code:
echo "this is a test" >/tmp/testfile ; ls -l /tmp/
My elderly "Unix in a nutshell" gives different meanings for & and ;
& Run process in background
; Separate commands on same line

With the ; the echo command should terminate before the ls command starts (I believe)
Possibly more certain would be to use && to make the ls only run if the echo terminated successfully.
 
Old 02-01-2013, 09:26 AM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by impert View Post
Code:
echo "this is a test" >/tmp/testfile & ls -l /tmp/
The OP had:
Code:
echo "this is a test" >/tmp/testfile ; ls -l /tmp/
Yes, I know. I was presenting an example of something where "Worked fine when I tried it" is not a sufficient answer.
 
Old 02-01-2013, 11:35 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I think the OP somehow believes that both commands would run simultaneously (or the changes/effects would not be recognizable immediately after echo) since they are on a single line, but no it won't.
Code:
echo "this is a test" >/tmp/testfile ; ls -l /tmp/
is -conceptually- the same as
Code:
echo "this is a test" >/tmp/testfile
ls -l /tmp/

Last edited by konsolebox; 02-01-2013 at 11:38 AM.
 
  


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, echo redirection not to the last line but first Xeratul Programming 2 09-03-2012 03:05 AM
[SOLVED] multi-line output from a bash script using echo and tr Leo Simon Linux - General 7 08-15-2012 03:05 AM
echo two command output in the same line chuikingman Linux - Software 8 03-31-2011 07:20 PM
how to remove new line in bash using echo packets Programming 9 11-24-2009 10:00 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 01:08 AM


All times are GMT -5. The time now is 02:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration