How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file.

Because I'm actually a windows guy and new here so for illustration is sort of :

$ bash "echo ${PATH} & echo have a nice day!"

will do output, for example:

/usr/local/bin:/usr/bin:/bin:/opt/bin:
have a nice day!

as it is analogous to windows:

cmd /c "echo %path% & echo have a nice day!"

Sincere help is really grateful to my thankfulness..

Hi,

If you're using Bash, then the simplest way to do this is just to separate them with semi-colon ( ; ) characters. Commands separated by semi-colons will be executed one after the other, regardless of the success or failure of the preceding command.

Example:

$ echo "This is the output of the first command." ; /bin/ls -F / ; echo "This is the output of the third command."
This is the output of the first command.
bin/   dev/  home/                 lib/    lost+found/  mnt/  proc/  run/   srv/  tmp/  var/
boot/  etc/  installCDE.15787.log  lib64/  media/       opt/  root/  sbin/  sys/  usr/
This is the output of the third command.
$

There are a few other ways to do this. If you only want the next command in the chain to be executed if the previous command was a success, then you can use a logical 'and' operator, represented by two ampersand ( & ) symbols:

$ echo "First command." && /bin/true && echo "The two previous commands returned 0, so we get to run our third."
First command.
The two previous commands returned 0, so we get to run our third.
$

And lastly, if you only want a command chain to continue if the previous command fails rather than succeeds, you can use the logical 'or' operator, represnted by a double pipe ( | ) symbol:

$ echo "First command". || /bin/true || echo "This time, we won't see the third command."
First command.
$

Hope this helps.

you haven't read the question carefully yet

how to run these in bash command-line, must follow bash executable program as these are bash parameter and/or argument
$ bash ???

?
to be as the output mentioned above

Try using the -c option to bash.

Note as you want ${PATH} to be expanded by the bash script you should protect if from being expanded by you current shell. An easy way to ensure this is to enclose the command within single quotes for example:

bash -c 'echo ${PATH} ; echo have a nice day!'
1 Like

What you originally have is more like saying "Run echo ${PATH} in the background and (ignoring if it works or not) run echo have a nice day! in the foreground"

Your output may not always be consistent. In this very simple case it probably always will be but more complex calls could have the output from either displayed first or even interleaved if you have two long running processes that output multiple lines. Can you tell us what you are trying to achieve here?

If you have a cmd1 ; cmd2 ; cmd3 , these run sequentially. There is no return code checking done, it just runs one after the other.

If you have a cmd1 & cmd2 & cmd3 & , these run in the background (so disconnected from your STDIN (usually keyboard) and may independently write output to STDOUT (usually screen) ) and because cmd3 has a trailing & putting it in the background, you get dropped back to the shell (or calling program) and can carry on with other work. This may or may not be what you wanted.

If you have a mixture then you have to interpret what will happen. You can also add conditional execution, so cmd1 && cmd2 || cmd3 will run cmd1 . If there is a zero return code (deemed as success) then run cmd2 . If either cmd1 or cmd2 have a non-zero return code (deemed as a failure) then run cmd3

This can be quite compact but often can get complicated to interpret, so most people a re more explicit with something more like:

cmd1
RC=$?
if [ $RC -eq 0 ]
then
   cmd2
   RC=$?
fi

if [ $RC -ne 0 ]
then
   cmd3
fi

Does this help, or just confuse? Apologies if it is the latter.

Robin