26

I am using Ubuntu 12.10 and want to run a set of commands in the terminal, and from what i see in the instructions, these commands each start on a new line. I don't know how to do this in the terminal. I can't find what key to press to do the carriage-return to the next line.

2
  • 4
    You can separate commands by using the semi-colon ";", like cd home; mkdir test; ls -la
    – LnxSlck
    Dec 6, 2012 at 16:49
  • 9
    @LnxSlck: Though, it's often preferable to separate commands with && instead of ;. This way, if one of the commands fail, the remaining commands will not be run. So, using your example, if home doesn't exist, you won't accidentally create a test directory in the current directory.
    – hammar
    Dec 6, 2012 at 19:11

6 Answers 6

28

As Web-E explains the most direct way to do what you want with two different commands, I thought I'd show that there are a number of ways to execute multiple commands or to continue commands onto another line without immediately executing them.

Continuing long commands:

1) The most common way to construct one long command is to enter your commands, then use a backslash \, press return, and then Bash will provide another prompt for you instead of executing the command. This secondary prompt is called PS2 and waits for your input:

find /home/mike/Downloads -type f -iname '*.jpg' \
> 

You can keep on adding backslashes and pressing return as long as you want, as long as you think the overall command will make sense.

You can cancel this secondary prompt with the usual Ctrl+C.

2) Bash recognises some commands such as for loops (for i in....) and the prompt will appear immediately; just as it will if you miss a quotation mark off a command:

apt-cache search 'libgimp*
> 

Multiple Commands:

3) As Lxnslck notes, you can separate commands with semicolons:

which vlc; whereis vlc

/usr/bin/vlc
vlc: /usr/bin/vlc /etc/vlc /usr/lib/vlc /usr/bin/X11/vlc /usr/share/vlc /usr/share/man/man1/vlc.1.gz

4) Or you can use the ampersand && to join two commands:

./configure && make
16

The commands you see in each line are to be executed one by one.

So after entering a line, press enter to execute then execute next command.

Example:

sudo apt-get update
sudo apt-get upgrade

These are two commands to be executed one by one.

To execute at once it will be like sudo apt-get update && sudo apt-get upgrade (there are other methods also), but these thing doesn't require generally . So don't worry. Type one line and press enter it will do it's job.

2
  • Thank you very much for your advice,, I wish I had known this earlier!!! Still, we live and learn :-) Dec 6, 2012 at 16:54
  • 4
    @user113090 If Web-E answered your question, don't forget to mark his answer as the accepted answer.
    – Dean
    Dec 7, 2012 at 3:02
4

You can press the ENTER key after each line and if the command is not terminated (mutiline commands like for loops for example), the terminal will wait for you to enter the rest of the command. If the command is terminated, it will be executed and you enter next command after, no problem.

If you are copying the commands from a tutorial, you can copy the whole group of commands and paste it directly in the terminal and it will work.

0
1

When it's a set of commands you expect to be using more than once, you should put them in a bash script file. For instance,

xrandr --newmode "1280x960_80.00" 139.25  1280 1368 1504 1728  960 963 967 1008 -hsync +vsync
xrandr --verbose --addmode VGA1 "1280x960_80.00"
xrandr --output HDMI1 --off --output LVDS1 --mode 1366x768 --pos 1280x512 --rotate normal --output DP1 --off --output VGA1 --mode "1280x960_80.00" --pos 0x0 --rotate normal
sleep 3
xfce4-panel -r

is nothing you'd like to type ever again, but I happen to need this particular sequence of commands very often. So it goes in a file called 1280x980-2head.sh, you can create that with any editor of your choice. To make that script executable, you put the line #!/bin/bash in front of everything (a shebang), and set the execution-permission: chmod +x 1280x980-2head.sh. Then, just typing ./128<tab> (auto-completion) and <enter> executes all the commands.

1

Back slash (\) and Enter is what you're looking for. Example:

Shell:> command --param1 paramValue \
--param2 paramValue \
--param3 paramValue \

When you press 'enter' after a '\' it will continue the command, but on a new line.

1
  • Those are regular slashes, not backslashes. Actually using a backslash \ instead would work, and you don't need to shift the enter key.
    – tripleee
    Nov 30, 2020 at 17:52
0

You can put everything in brackets; for example:

(sudo add-apt-repository ppa:foo/bar
sudo apt-get update
sudo apt-get install my-package)

The commands will be executed one after the other in a sub-shell.

If you don't want them to be executed in a sub-shell, you can put a semicolon between a command and the other; for example:

sudo add-apt-repository ppa:foo/bar; sudo apt-get update; sudo apt-get install my-package

Instead, if you put "&&" between each commands, the command that follows the "&&" will be executed only if the previous command has finished with no error, so you don't have to use it to concatenate commands; use semicolons instead. Conversely, if you put "||", the following command will be executed only if the first one exits with an error. Example: (the "which" command checks if a program exists, and if doesn't exits with an error)

which apt-get && echo "No error" || echo "Error"    # Will print "No error"
which jdhdsd && echo "No error" || echo "Error"  # Will print "Error"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .