Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute three commands in single command line in Linux

Tags:

linux

How can I execute three commands on one command line in Linux? I tried the below:

sudo -u oracle -i ; cd /lo2/ram/daska; ./script.sh

When I execute this only the sudo command is executing.

Please advise me

like image 726
radas Avatar asked May 13 '15 09:05

radas


People also ask

How do you write multiple commands in one line in Linux?

Linux allows you to enter multiple commands at one time. The only requirement is that you separate the commands with a semicolon. Running the combination of commands creates the directory and moves the file in one line.

How run multiple commands in Linux?

Repeating a Command Using 'for' Loop You can use a 'for' loop to print a certain command multiple times. There are different variations of the 'for' loop, and we will explore all of them with the help of different bash scripts.


2 Answers

Use && separator

sudo -u oracle -i && cd /lo2/ram/daska && ./script.sh
like image 162
vgonisanz Avatar answered Oct 17 '22 00:10

vgonisanz


After executing sudo there's a new shell and the rest of "commands" are not part of it but part of the parent shell. You can do:

 sudo -u oracle -i bash -c "cd /lo2/ram/daska && ./script.sh"

Or directly,

 sudo -u oracle -i /lo2/ram/daska/script.sh
like image 3
P.P Avatar answered Oct 17 '22 00:10

P.P