How do I run a command?
I am a new user, who has just been told to run / execute a command. The answer has some text with a grey background like this text here
or
sometimes this text here.
However, I don't know what "run" or "command" means in relation to Ubuntu.
How do I do this?
This is intended for use as a Q&A you can link to when you tell someone to run a command.
Solution 1:
What is a command?
Commands, and the command line, are another way to tell the computer what to do. For example, you can click the google chrome icon, or you can run the command google-chrome
.
This may seem a silly thing to do, but when you click the icon, that's what the computer does! It also is much easier to do some tasks with the command line.
For example, installing programs can be quicker and easier - just use sudo apt-get install program
, rather than navigating through the software center.
And if my Bluetooth stops working, I run a command to fix it - that's the only way!
Anyway, how do you run a command?
This is also simple, although there are many ways to do it.
Simply, you open a Terminal and type (or copy / paste) the command in, then press Enter. In the image, I've run the command ls Desktop/
which will list all files on my desktop. Note that to paste you press Ctrl + Shift + V in the terminal.
By default you open the terminal by pressing Ctrl + Alt + T, or opening the Dash and searching for "Terminal".
What is a tty
?
Sometimes, you may be told to run a command in the tty
- often followed by a number, for example tty2
. The GUI runs on tty7
. (Update for 2021: the GUI now often runs on tty1
or tty2
).
To access this, you press Ctrl + Alt + FX - where FX is replaced with the Function key that corresponds to the number. For example, to access tty2
you would press Ctrl + Alt + F2.
You have to login to this, but some things need the tty
to work.
Getting back to the GUI will depend on your Ubuntu version, but trying each tty
in turn should work until you find it. If you can’t find it on any tyy
1-8, you may need to restart - but ask the person who is helping you by commenting below the answer!
What about GUI (Graphical User Interface) programs?
The terminal can also launch GUI programs. This can have the advantage of viewing the output the program gives - for example running software-centre
looks like this:
You can also provide extra "arguments" - also known as "parameters" and "options" to the command.
For example, if I run google-chrome --app=http://youtube.com
it opens a window without the URL bar or the tabs:
Bear in mind, if you close the terminal, you also lose the Google Chrome window...
One final method
This is only good for GUI programs, like Chrome, Firefox and Gedit. However, it also means you don't have a terminal lying around - which you could accidentally close. It's no good for sudo apt-get install program
or similar, because you can't type your password after running it.
Press Alt + F2, and a "Run Dialouge" appears - a bit like the run window on Windows:
Ubuntu: Windows:
You can type any command into here, and it will run it!
Solution 2:
Types of commands vary somewhat. They can be single line, like
echo 'HelloAskubuntu' # Print 'HelloAskubuntu'
single line with redirection to other commands
echo 'Hello World' | wc # count number of lines, words, and chars in 'Hello World'
multiline with redirection
$> bash << END
> echo one
> echo two
> echo three
> END
one
two
three
Or multiline in form of scripts (which in terms of Windows language is batch files). Scripts are just files with lists of commands in them, that are executed sequentially. These are effectively custom build programs/software, analogous to building your own electronics out of existing spare parts.
Regardless of the type of command, you will need access to a command line, commonly known as Terminal
. There is either TTY terminals on Ctrl+Alt+F[1-6] keyboard shortcuts (exit those with Alt+F7), or you can use GUI app by pressing Ctrl + Alt + T.
Terminals typically run software known as shell
. The shell interprets the commands and executes them. Some shells are compatible with each other for the most part like bash
, ksh
, mksh
, zsh
, while tcsh
and csh
are quite different. They all have different features, and often are used for different tasks.
Either a script or a command have to be executed in shell. Some commands are physically present executable files on disks, like /bin/ls
. Other commands are aliases
or functions
that are lists of existing commands. They exist only in the settings of the shell. For example, for bash it would be .bashrc
settings file. Real executables can be run with the name or full path, like ls
or /bin/ls
respectivelly.
Scripts can be run with full path, or if they are stored somewhere that your shell knows about ( and it knows about only things in the PATH
variable, which you can read with echo $PATH
). For example, I can run my script by going to my bin directory and running it with ./
operator, like so
cd $HOME/bin; ./helloworld.sh
Or give terminal full path
/home/Serg/bin/helloworld.sh
Solution 3:
To run a command you need to open a terminal window. You can find the terminal among your applications like you would any other. Copy and paste the grayed text into the terminal window after opening it and then press Enter key to execute. You will probably see some text output during and after execution. This is feedback from the command you just ran.
Command is an instruction you give to a computer - you tell it to do something. You are already familiar with some commands, you use them while using your computer all the time. For example clicking on buttons in your GUI (graphical user interface), like web browsers, is the same thing - telling your computer to do something.
If you require more information you can find it at What is a terminal and how do I open and use it? and https://help.ubuntu.com/community/UsingTheTerminal