Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I could (1) prepare a file with typed commands seperated by end-line, (2) make it executable, (3) run it from a file-system manager or the terminal.

But this is ridiculous for not repeatable and every-time-other sets of commands.

Can I type those commands to the terminal in one request instead?

I don't know end-line character for the terminal - Ctrl, Shift or Alt with Enter doesn't work.

share|improve this question
1  
On a command line, commands can be separated with a semicolon. –  John1024 Feb 1 at 2:55

3 Answers 3

up vote 2 down vote accepted

You can separate commands with && or ;.

  • && only runs the next command if the previous one exited with status 0 (was successful) :

    command1 && command2 && command3
    
  • ; runs every commands, even if the previous one exits with a non zero status :

    command1; command2; command3
    

You can combine these separators as you wish.

share|improve this answer
4  
for command1 && command2 command2 will only be executed if command1 is successful. –  souravc Feb 1 at 3:18
    
Nice discussions, relevant/similar posts: askubuntu.com/questions/334994/… stackoverflow.com/questions/13077241/… –  gevang Feb 1 at 4:04
    
@souravc : I made an edit, thanks, I learned something. –  MrVaykadji Feb 1 at 10:27

If you are interested to type each command on its own line in one single request you can use the following method:

  • Start your request (first line) with if :; then (this mean: if true, then do) and press Enter; your prompt will change now in > and nothing will be executed.

  • Type your commands, each one followed by Enter

  • Finish your request with with fi (end of the above if condition) and press Enter. Now all your commands will be executed in the given order.

Example:

radu@Radu: ~ $ if :; then
> echo 'something'
> echo 'something else'
> echo 'List current directory contents:'
> ls
> echo 'Change current directory with root directory:'
> cd
> #finish
> fi
something
something else
List current directory contents:
Backups            Desktop           forma3d  Public      Untitled txt.txt~
bin                Documente         Music    Templates   Videos
configuration.php  examples.desktop  passwd~  tmp~
Downloads          file~             Poze     Ubuntu One
Change current directory with root directory:
radu@Radu: / $
share|improve this answer
    
if true; then may be clearer to read if so desired. : may be confused with the ; at first glance. –  minerz029 Feb 1 at 10:45

First, put a { on its own line.
Then, insert your commands.
Then, put a } on a new line and press Enter. Your commands will be executed.

Example:

{
echo list
echo of
echo commands
echo to run at once
}

which will print (all at once, with no prompt in between):

list
of
commands
to run at once

As a side note, { .. } is the Bash command grouping syntax. It's often useful in conjunction with && or || ('and', and 'or' respectively)

share|improve this answer
    
Is that the same that if :; then already mentionned ? Or is it slightly different ? –  MrVaykadji Feb 1 at 10:38
1  
@MrVaykadji It's the same outcome, but a different method. if : runs a test on the null command, which will always return true. { .. } just groups the commands together. I personally find { .. } easier to remember. –  minerz029 Feb 1 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.