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 01-22-2018, 05:54 PM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
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..
 
Old 01-22-2018, 06:20 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
Quote:
How to run several bash commands put in bash command line without needing and requiring a script file.
Code:
for i in {1..10}; do echo $i; sleep 1; done
 
Old 01-22-2018, 06:41 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by BudiKusasi View Post
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!"
It depends on what you want as output, and whether you want the second part to depend on the first.

To get the output based on your example, something like one of these would work...

Code:
echo ${PATH} && echo "Have a nice day!"
echo ${PATH}; echo "Have a nice day!"
The && is a conditional and operator, often called a short-circuit operator, and means that if the part on the left succeeds then do the part on the right, so execution of the second is conditional, it depends on the success of the first (see below).

The ; denotes the end of a command and allows you to put multiple commands on a single line, without dependence on each other.

Conditional expressions are more useful for constructs other than simply echoing output (which presumably always succeeds). You might try something like this:

Code:
((1<0)) && echo "Have  a rainy day"
((1>0)) && echo "Have  a nice day"
If the math condition in ((..)) is true, then the string will be echoed, otherwise not.
 
Old 01-22-2018, 08:09 PM   #4
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,993

Rep: Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628
I might as well confuse the issue with expect and autoexpect.
 
Old 01-22-2018, 08:31 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,337
Blog Entries: 28

Rep: Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144Reputation: 6144
If they are sequential commands, you can use "&" and "&&." If you use "&&" the system will wait for the first command to finish before starting the second one. Here's an example I use frequently:

Code:
apt-get update && apt-get upgrade
 
Old 01-22-2018, 10:41 PM   #6
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
Code:
echo $PATH & echo have a nice day
Will execute echo $PATH in the background and echo have a ... immediately in the foreground. Which isn't what you want
 
Old 01-22-2018, 11:27 PM   #7
giis
Member
 
Registered: Nov 2013
Location: Third Rock from Moon
Distribution: RPM/DEB based and LFS
Posts: 73

Rep: Reputation: Disabled
Quote:
Originally Posted by BudiKusasi View Post
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..
You can use semi-colon as separator like "pwd;ls;cd /tmp"

But I high recommend placing them inside script file. Bash script is mostly collection of Linux commands.
 
Old 01-23-2018, 04:05 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
not to speak about -c:
Code:
bash -c "command1; command2"
- or
bash -c "command1 && command2"
 
1 members found this post helpful.
Old 01-23-2018, 02:39 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
This thread demonstrates how a simple question with somewhat vague parameters will be seen differently by each person who reads it.

It would be helpful to us and to themselves if the OP would return and clarify exactly what they are trying to accomplish, with their actual use case. Similarity to a window$ use case is not a sufficient requirements list!
 
1 members found this post helpful.
  


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
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
[SOLVED] Partial list with ls-l in bash script run in cron but full list run from command line redgshost Linux - General 29 01-16-2011 12:14 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
BASH Script, command line, same commands, different returns (need help). SilversleevesX Linux - Newbie 4 02-04-2010 03:46 PM
Does bash shell parameter expansion run the risk of overflowing command line buffer? david1941 Linux - Software 1 04-28-2009 10:13 PM

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

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