- 1 Introduction
- 2 Command Navigation
- 3 Miscellaneous Command Line Functions
- 4 Scripting
- 5 Other shells
- 6 References and Further Reading
The purpose of this document is to give you an understanding of some of the advanced features of the command line. This document is going to assume you have read the CommandlineHowto. See also the excellent documentation listed in references [[UbuntuHelp:[1|1]]] and [[UbuntuHelp:[2|2]]].
Here is a list of commonly used keyboard shortcuts using the default shell, bash (/bin/bash):
Up Arrow & Down Arrow: previously used commands in the current session.<
> Ctrl-A: Move cursor to beginning of the current line.<
> Ctrl-E: Move cursor to the end of the current line.<
> Ctrl-U: Clear the current line. <
> Ctrl-H: Same as backspace.<
> Ctrl-K: Delete all the line from the current cursor position.<
> Ctrl-W: Delete the word before the current cursor position.<
> Ctrl-D: On a blank line is the same as the exit command. Otherwise, it deletes the character in front of the cursor.<
> Ctrl-C: Stop the current running command.<
> Ctrl-Shift-C: Copy.<
> Ctrl-Shift-V or Shift-Insert: Paste.<
> Tab: command completion.<
The history command is a handy tool that allows you to view/use commands that you have already used. One advantage of this tool is when you have to use the same command multiple times. This becomes useful when you a troubleshooting an issue with the system or when you are trying to configure a feature in Linux. Unlike the Up Arrow and Down Arrow keys, as seen in the previous section of this document, history will store all commands used as opposed to the commands used in the current session of terminal. Here is an example of using the history command.
localuser@localhost:~ $ history 1 ls 2 cd / 3 ls 4 history localuser@localhost:~ $!1 test1.pdf test2.txt test3.gif localuser@localhost:~ $
As you can see in the example above, just the history command will list the last used commands. Also in the example above you are able to execute a previous command by using ! followed by the command number. !! alone will execute the most recent command again.
Miscellaneous Command Line Functions
Here are a couple of miscellaneous functions to help use the command line easier.
The environment variables are global variables that can be used by all commands. Some of the information stored in the variables is common information as well as system information. An example of an environment variable is PATH, which contains the location of all executables. Using the env command allows you to list as well as modify the environment variables. When you use just the env command alone it will list all the environment variables. Here is an example on how to modify an environment variable:
localuser@localhost:~ $ env HOME=/usr/users/pmd bash
This example will change your home directory before starting a subshell.
The alias command allows you to define a new command. Here is an example of using the alias command:
localuser@localhost:~ $ alias l='ls -r'
This example will allow you to use l if you want to see the directory in reverse order.
Scripting is the combining of commands into a logical file to complete a specific task. Example of scripts would be a login script that allows you to be able to start programs or maybe setup variables that are used only by that user. Assumed knowledge for this page is that you can use an editor.
Within the Linux world, much debate has been had on the topic of which is the best editor. Each has their flaws and their advantages, but the only way to determine which is the one for you is to try them, find one you like, and learn how to use it properly. Here is a list of the most commonly used editors. Links have been provided below for additional information about each of the editors.
- Emacs Emacs is described by the GNU project as "the extensible, customizable, self-documenting real-time display editor." Part of Richard Stallman's GNU project, emacs often suits the newcomer, but has hidden depths which require careful study to release the editor's full capabilities. Note: emacs is not included in a standard Ubuntu installation, and must be installed from the Universe Repository. Emacs Homepage<
- Vim-gnome Vim-gnome is the version of the vim editor provided by Ubuntu. The homepage describes Vim as "an advanced text editor that seeks to provide the power of the de-facto Unix editor 'Vi', with a more complete feature set." Unlike most other editors, Vim is a dual-mode editor, where one mode is used for document editing, while a different mode is used to actually insert text. This can seem daunting to new users, but, just like emacs, the editor has powerful features which need to be learned. Note: Vim-gnome is not installed by default in Ubuntu, but it is available from the Installation CD. Vim Homepage<
- nano The homepage proudly declares that "GNU nano is a small and friendly text editor." Unlike emacs or vim, there is very little to learn about nano, you can type scripts, you can find/replace, you can get started quickly. It does lack some(most) of the features needed by dedicated coders, but it has its own strengths - most especially its tiny size and memory requirements, which mean that it will almost always be installed on any UNIX-based environment. GNU nano is, of course, installed as part of your Ubuntu installation. Nano Homepage<
Each time you type on the command line you can save what you are doing and repeat it any number of times by creating a file called a script. You can simply edit a series of repeated commands into a file. Edit a file called reverse and place the following lines in it:
We now need to change this file to be executable:
$ chmod +x reverse
We can now execute this file:
You should see the directory listing in reverse. There are, of course, many simpler ways to achieve this result; this is meant as a simple example. The script above will run in the command environment of the user. Because this is a very simple script it will work in all shells that I know of. You can also use specific notation to instruct your computer which program to use for your script. See the following example:
#!/bin/bash echo "Hello world"
This simple script uses two lines. The first informs your system that this script uses the bash program, located at /bin/bash. This is accomplished with the use of '#!' This is commonly called 'hash-bang'. The second is the command passed to the /bin/bash/ program, in this case 'Hello World' is printed on your terminal.
Other Scripting Languages
perl(Practical Extraction and Report Language) [[UbuntuHelp:[3|]]] is a general purpose scripting language created by Larry Wall. One of Perl's strengths is text manipulation.
Python [[UbuntuHelp:[4|]]] is a powerful object-oriented scripting language.
Generally other shells are not recommended; they are mainly there for compatibility with other systems. For example, you may be developing for an AIX Unix system whose default shell is ksh; thus, you would test under ksh rather than bash.
ksh [[UbuntuHelp:[5|]]] or the korn shell is one of the early advanced shells. Code written in ksh almost always runs in bash without changes, but not the reverse. It is a common default shell for commercial Unix distributions.
The (t)csh [[UbuntuHelp:[6|]]] scripting language was based around the C syntax and was considered easier to use than the old bourne shell script. Largely this shell is used to support existing applications written a long time ago. Openoffice.org, for example, uses tcsh shell scripting for historical reasons.
zsh [[UbuntuHelp:[7|]]] is an advanced scripting language. It has better command line completion than bash.
References and Further Reading
1. <<Anchor(1)>> Introduction to Bash Scripting <
> 2. <<Anchor(2)>> Advanced Bash Scripting Guide<
> 3. <<Anchor(3)>> Learning perl<
> 4. <<Anchor(4)>> Python<
> 5. <<Anchor(5)>> (pd)ksh<
> 6. <<Anchor(6)>> (t)csh<
> 7. <<Anchor(7)>> zsh<