From time to time you will come across the term "command line" on this site. That refers to a little but powerful program called the shell that is running in a so-called terminal window. Traditionally the usage of the shell was common only for system administrators and developers but nowadays also designers and web developers usually need a basic unnderstanding of the shell.

Starting a Shell

On the Mac start the Spotlight search field with CMD-Spacebar and enter "Terminal".

There are countless terminal programs availabe for Linux. Their program icon usually resembles an old CRT monitor, often with stylized prompt >_ or $_. You will usually find it under "Tools", "Accessories", "Utilities", or the like.

Typical Icon For A Terminal Program
A Terminal Icon

Windows still ships without a fully fledged shell. You have to make do with the program cmd.exe. You find it via Start, when you enter cmd.exe followed by Enter.

On all systems a mostly empty window will open. The last visible line is normally terminated by a dollar sign, followed by an underscore or rectangle, sometimes blinking, the so-called prompt. The text between the beginning of the line and the dollar sign usually contains the user name, the computer name, and a part of the current directory name.

Terminal Window On the Mac
A Mac OS X Terminal Window

Because the text in front of the dollar sign varies from system to system and is also configurable, most instructions in the web just display a lone dollar sign here.

Entering Commands

Shell commands are normally smal utility programs that are only suited for a command line environment. You invoke them by name. For example, the program resp. command ls exists on every unix system:

{% highlight shell %} guido@macbook:~ $ ls Applications Documents Library Music Public Desktop Downloads Movies Pictures guido@macbook:~ $ {% endhighlight %}

The command ls lists the content of the current working directory. The Windows counterpart to ls is the command dir dir.

Arguments and Options

You can alter the behavior of commands with options and arguments, that have to be separated by spaces from the command and from each other:

{% highlight shell %} guido@macbook:~ $ ls -l /Users/guido total 13296 drwx------ 6 guido staff 204 Feb 23 13:37 Applications drwxr-xr-x+ 30 guido staff 1020 Jun 5 15:36 Desktop drwxrwxr-x+ 162 guido _appserveradm 5508 Jun 4 10:27 Documents drwxr-xr-x+ 70 guido staff 2380 Jun 5 14:49 Downloads drwx------@ 68 guido staff 2312 May 16 08:45 Library drwx------+ 10 guido staff 340 May 5 19:22 Movies drwx------+ 12 guido staff 408 Feb 4 09:06 Music drwx------+ 7 guido staff 238 Jul 28 2015 Pictures drwxr-xr-x+ 5 guido staff 170 Aug 20 2014 Public guido@macbook:~ $ {% endhighlight %}

Options always begin with a hyphen, everything else are arguments. In the example above ls was invoked with the option -l that produces a long, verbose output. /Users/guido is an argument. For the command ls arguments contain the directories or file names to be listed.

Many programs allow not only short options with one hyphen and one character but also long options with two hyphens and a meaningful name. By convention the options -h or --help will display a help page.

On linux systems you can normally mix options and arguments, other systems usually do not support that, and you should always give options first, and arguments last.

Windows behaves totally different. Options usually begin with a slash (/), and help gets requested with /?.

Change Directory

You will often be asked to change into a certain directory, for example /var/log/messages. You achieve that with the command cd like change directory:

{% highlight shell %} guido@macbook:~ $ cd /usr/bin/ guido@macbook:/usr/bin $ {% endhighlight %}

The command cd also exists under Windows but there you have to separate path components by a backslash (\) instead of a (forward) slash (/).

Typing long path names is error prone and sometimes you don't even know the complete path name. You may try out the following: At the command-line prompt type cd including a trailing space but with hitting Enter, then you open a Finder window on the Mac (a Windows Explorer under Windows), navigate to the directory that contains your target directory, you grab the target directory's icon with the mouse and drag it into the terminal window. Normally you will now read the complete path name there and you just have to hit Enter.

Execute Commands With Administrator Privileges

Certain commands require superuser privileges. On the Mac or under Linux you simply have to prepend sudo followed by a space character:

{% highlight shell %} guido@macbook:/var $ ls /var/backups ls: backups: Permission denied guido@macbook:/var $ sudo ls /var/backups Password: file1 file2 file3 guido@macbook:/var $ {% endhighlight %}

When prompted for a password, give your own password. This password gets cached for a certain time, and if you use sudo again, you will not be prompted again for a password.

Alternatively, you can impersonate the administrator for the time of your session with the command su or better su -. You will be prompted for the root password then. In case you don't know it (for example on the Mac or under Ubuntu Linux), the command sudo su - may have the same effect after you gave your own user password instead.

Because the prompt for the unix superuser root normally ends with a hash sign instead of a dollar sign, you will often see the hash sign instead of the dollar sign. That often means that you will need superuser privileges for the commands described there.

Windows does neither have su nor sudo. Instead, you have to start the terminal with administrator privileges. Click Start, and after entering "cmd.exe" into the search box, hit CTRL-Shift-Enter instead of just Enter.

There are countless tutorials in the web that give you more detailed information about the shell, just google for "shell tutorial". The most popular interactive shell is probably "Bash", and therefore you can use "Bash" almost as a synonym for "shell".

Leave a comment
This website uses cookies and similar technologies to provide certain features, enhance the user experience and deliver content that is relevant to your interests. Depending on their purpose, analysis and marketing cookies may be used in addition to technically necessary cookies. By clicking on "Agree and continue", you declare your consent to the use of the aforementioned cookies. Here you can make detailed settings or revoke your consent (in part if necessary) with effect for the future. For further information, please refer to our Privacy Policy.