LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash: echo followed by command on the same line ??? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-echo-followed-by-command-on-the-same-line-4175447966/)

RandyTech 01-31-2013 04:56 AM

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' :)

colucix 01-31-2013 05:04 AM

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.

grail 01-31-2013 06:34 AM

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.

shivaa 01-31-2013 09:26 AM

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?

rknichols 01-31-2013 10:43 AM

Whether the two commands are separated by ";" or by a newline makes absolutely no difference. They are still entirely separate commands.

vipin310379 01-31-2013 11:09 AM

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/

David the H. 01-31-2013 04:32 PM

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.

impert 01-31-2013 04:39 PM

why didn't you just try it? I did, it worked like a charm

rknichols 01-31-2013 05:06 PM

Quote:

Originally Posted by impert (Post 4881677)
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.

impert 02-01-2013 05:38 AM

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.

rknichols 02-01-2013 08:26 AM

Quote:

Originally Posted by impert (Post 4882128)
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.

konsolebox 02-01-2013 10:35 AM

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/



All times are GMT -5. The time now is 07:51 AM.