Basic commands
1 Basic Commands
First let’s cover some basic principles.
1.1 UNIX and Windows Differences
The type of operating system we are using will affect the commands we use. We can refer to this as the “flavour” of commands we use. Certain programs were designed for specific operating systems and carry this flavour with them.
The program we use to interact with the command line will determine which flavour we use. In principle, each flavour can achieve the same thing, but with slightly different commands.
Below is a breakdown of which program is which flavour. Throughout this course we will give commands for each flavour: UNIX-like and Windows. UNIX-like are typically programs designed for MacOS or Linux, but can be found on Windows machines. Choose the one that is right for the program you are using.
UNIX-like | Windows |
---|---|
Git Bash | Anaconda Prompt |
Terminal (MacOS) | Windows Command Prompt |
Bash | Power Shell |
UNIX-like command line interfaces typically allow you to type following the $
symbol in the program, whereas Windows flavours use a >
.
1.2 Comparison of different Shells
- Command line for Microsoft Windows operating system, with command-based features
- Inputs/Outputs treated like characters
- Text-based command-line interface
- Default program in all Windows OS
- Task-based command-line interface, based on .Net Framework and designed for system admins
- Treats input and output as objects (similar to Python/Java)
- More interactive, graphical command-line interface than CMD
- Built-in to Windows 7 and above
- Command-line and scripting language for Unix/Linux-based OS
- Inputs/Outputs treated like characters
- Text-based command-line interface
- Default for Linux/Unix systems
Extends the standard command line-interface of your OS to use anaconda and conda commands (See the Anaconda docs for more detail)
NOTE As we are using networked/restricted devices, some of the things we can generally do in command line are not allowed on our computers. Some programs for accessing the command line do not allow the full functionality as they are designed for a specific purpose. For example: Git Bash allows basic directory commands and accessing Git, but not things like running other programs.
For Windows users it is useful to learn both the UNIX-like and Windows syntax, but first learn one.
If you are on a Mac, please use the Terminal for this course, if you are on a Windows, please use Command Prompt.
1.3 Opening the Command Prompt/Terminal
Either open the utilities folder and find the terminal icon (shown below) or search the system for ‘terminal’
In the start menu, either search for ‘cmd’ (shown below) or find the icon under the Windows System folder.
If you are on a Windows machine and are unable to access a UNIX-like command line interface (such as Git Bash) you can interact with a terminal online here if you would like to practice this flavour.
1.4 Command Syntax
Typically, you use the command line by typing a single line at a time. The computer interprets and actions each line after you type the Enter
key. Most command line interpreters also allow for the running of scripts (multiple lines of commands) to run through a sequence of actions, allowing it to be used as a programming language.
We will show and list some general syntax when using the language without specifying actual commands. You will see commands referred to online and in literature using a similar manner, but convention varies.
The command line interpreter (Bash, Windows Command Prompt, Git Bash etc.) will start a line with either $
or >
depending on flavour. For simplicity in this section we will start lines with $
.
We typically start a command with text representing either a program or instruction. Required text is denoted without any wrapper (such as brackets, quotation marks, etc).
$ command
If we wanted to do this command to a certain object, we would add another argument. Arguments are separated by spaces. Placeholders are commonly denoted with angle brackets (<
and >
) surrounding the object.
$ command <put your argument here>
If we wanted to do the command to the object called test_object
we would type:
$ command test_object
Note that there is no <
>
as we just use that to represent a placeholder, not a real object.
Optional arguments for a command are denoted with square brackets surrounding them. Our command therefore becomes:
$ command [optional argument goes here] <put argument here>
We typically put optional arguments not at the end by convention.
Implementing this syntax with the optional argument -o
becomes:
$ command -o test_object
-o
is called a flag, which is a type of optional argument. Flags have a dash -
before their name. You will see these come up frequently in UNIX-like systems. They are a type of argument that modifies the behaviour of what the command does in some way. The Windows alternative is adding an argument with /
such as command /A
.
If you are coming to this course with some experience in other programming languages, you can consider command line commands similar to functions.
First, you write the function name (the base command), then you add required arguments for the function to run, then add optional parameters. Instead of using commas to separate arguments, we use spaces.
To summarize:
- write a command in the command line,
- each command statement has some instruction, and can have arguments and optional arguments depending on the command,
- each part of the command statement is separated by a space,
- to run the command statement press
Enter
once it is fully written.
In addition, you can autocomplete commands and file names by pressing the Tab key.
Instead of rewriting the same instruction or similar multiple times you can press the up arrow in command line to retreive a previously executed command. You can keep pressing up to go further and further back into previous commands.
1.5 Help
Before we get to writing commands, we will first mention how to get more information about the commands we want to use.
You have two options when looking for more information in terminal/bash. The first is:
$ man <command>
man
is short for “manual”, which tells you information about the command you are specifying, including optional arguments.
Many commands also supply a -h
or -help
flag which will return useful information about the command.
$ <command> -h
We can use the help
command to get more information. If we do not supply an optional argument then the command will return a list of other commands that can be executed.
If we add an optional command following help
it will return us the help information supplied for that command, if it exists.
>help [command]
1.6 Exercises
Open your command line interface.
Get help for the following command:
cd
Question 1 Open your command line interface.
Open your search bar and select “Terminal”. Run the program.
Question 2
Get help for the following command: cd
$ cd -h
Question 1
Open your command line interface.
Open your search bar and select “Command Prompt”. Run the program.
Question 2
Get help for the following command: cd
>help cd