After the character-device driver processes what you type, the command line is passed to a command interpreter or shell. The default shell is sh, which under BlackBerry 10 OS is a link to the Korn shell, ksh .

In general terms, the shell breaks the command line into tokens, parses them, and invokes the program or programs that you asked for. The specific details depend on the shell that you're using; this section describes what ksh does.

As you type, the Korn shell immediately processes the keys that you use to edit the command line, including completing commands and filenames. When you press Enter, the shell processes the command line:

  1. The shell breaks the command line into tokens that are delimited by whitespace or by the special characters that the shell processes.
  2. As it forms words, the shell builds commands:
    • Simple commands, usually programs that you want to run (for example, less my_file)
    • Compound commands, including reserved words, grouping constructs, and function definitions

    You can also specify multiple commands on the command line.

  3. The shell processes aliases recursively.
  4. The shell does any required substitutions, including parameters, commands, and filenames.
  5. The shell does any redirection.
  6. The shell matches the remaining commands, in this order: special builtins; functions; regular builtins; executables.

To override the order in which the shell processes the command line, you use quoting to change the meaning of the special characters.

The sections that follow give the briefest descriptions of these steps— ksh is a very powerful command interpreter! For more details, see its entry in Utilities.

Editing the command line

The Korn shell supports emacs-style commands that let you edit the command line.

If you want to: Press:
Move to the beginning of the line Ctrl A
Move to the end of the line Ctrl E
Move to the end of the current word Esc F
Move to the beginning of the current word Esc B
Delete the character at the cursor Ctrl D
Delete the character before the cursor Ctrl H
Delete from the cursor to the end of the current word Esc D
Delete from the cursor to the end of the line Ctrl K
Paste text Ctrl Y

As in emacs, commands that involve the Ctrl key are keychords; for commands that involve Esc, press and release each key in sequence. For more information, see emacs interactive input-line editing in the documentation for ksh.

To process these commands, ksh uses the character device in raw mode, but emulates all of the driver's processing of the keys.

Command and filename completion

You can reduce the amount of typing you have to do by using command completion and filename completion. To do this, type part of the command's or file's name, and then press Esc twice (that is, Esc Esc ) or Tab once. The shell fills as much of the name as it can; you can then type the rest of the name—or type more of it, and then press Esc Esc or Tab again.

For example, suppose your system has executables called my_magnificent_app and my_wonderful_app:

  • If you type my_ followed by Esc Esc or Tab, the shell can't complete the command name because what you've typed isn't enough to distinguish between the possibilities.
  • If you type my_w followed by Esc Esc or Tab, the system completes the command name, my_wonderful_app.

If you haven't typed enough to uniquely identify the command or file, you can press Esc = to get a list of the possible completions.

You can control which keys the shell uses for completing names by setting the shell's complete key binding. For example, the command that lets you use the Tab key is as follows:

bind '^I'=complete

You can use bind on the command line or in the ksh profile. For more information about the bind command and the key bindings, see emacs interactive input-line editing in the documentation for ksh in Utilities.

Reserved words

The Korn shell recognizes these reserved words and symbols:

case   else   function   then    !
do     esac   if         time    [[
done   fi     in         until   {
elif   for    select     while   }

and uses them to build compound commands. For example, you can execute commands in a loop:

for i in *.c; do cp $i $i.bak; done

Entering multiple commands

You can enter more than one command at a time by separating your commands with a semicolon (;). For example, if you want to determine your current working directory, invoke pwd . If you want to see what the directory contains, use ls . You could combine the two commands as follows:

pwd; ls

As described in Pipes, you can also use pipes (|) to connect commands on the command line.


You can define an alias in the shell to create new commands or to specify your favorite options. For example, the -F option to the ls command displays certain characters at the end of the names to indicate that the file is executable, a link, a directory, and so on. If you always want ls to use this option, create an alias:

alias ls='ls -F'

If you ever want to invoke the generic ls command, specify the path to the executable, or put a backslash (\) in front of the command (for example, \ls).

Aliases are expanded in place, so you can't put an argument into the middle of the expanded form; if you want to do that, use a shell function instead. For example, if you want a version of the cd command that tells you where you end up in, type something like the following in ksh:

function my_cd
  cd $1

For more information, see Functions in the entry for ksh in Utilities.


The shell lets you use a shorthand notation to include the values of certain things in the command line. The shell does the following substitutions, in this order:

  1. Directories—tilde expansion
  2. Parameters
  3. Commands
  4. Arithmetical expressions
  5. Braces
  6. Filename generation

Let's look at these in more detail:

Directories—tilde expansion
The shell interprets the tilde character (~) as a reference to a user's home directory. The characters between the tilde and the next slash (if any) are interpreted as the name of a user.

For example, refers to some_file in the home directory of the user named mary. If you don't specify a user name, it's assumed to be yours, so ~/some_file refers to some_file in your home directory.

To include the value of a parameter on the command line, put a dollar sign ($) before the parameter's name. For example, to display the value of your PATH environment variable, type:
echo $PATH
Sometimes, you might want to execute a command and use the results of the command in another command. You can do it like this:

or with the older form, using backquotes:


For example, to search all of your C files for a given string, type:

grep string $(find . -name "*.c")

The find command searches the given directory (. in this case) and any directories under it for files whose names end in .c. The command substitution causes grep to search for the given string in the files that find produces.

Arithmetical expressions
To specify an arithmetical expression in a command line, specify it as follows:
$(( expression ))

For example:

echo $((5 * 7))

You're restricted to integer arithmetic.

You can use braces to add a prefix, a suffix, or both to a set of strings. Do this by specifying:

where commas (,) separate the strings. For example, my_file.{c,o} expands to my_file.c my_file.o.

Filename generation
Instead of using a command to work on just one file or directory, you can use wildcard characters to operate on many.
If you want to: Use this wildcard:
Match zero or more characters *
Match any single character ?
Match any characters (or range of characters separated by a hyphen) specified within the brackets []
Exclude characters specified within brackets !

Hidden files, whose names start with a dot (for example, .profile), aren't matched unless you specify the dot. For example, * doesn't match .profile, but .* does.

The following examples show you how you can use wildcards with the cp utility to copy groups of files to a directory named /tmp:

If you enter: The cp utility copies:
cp f* /tmp All files starting with f (for example, frd.c, flnt)
cp fred? /tmp All files beginning with fred and ending with one other character (for example, freda, fred3)
cp fred[123] /tmp All files beginning with fred and ending with 1, 2, or 3 (that is, fred1, fred2, and fred3)
cp *.[ch] /tmp All files ending with .c or .h (for example, frd.c, barn.h)
cp *.[!o] /tmp All files that don't end with .o
cp *.{html,tex} All files that end with .html or .tex

Redirecting input and output

You can override the behavior of commands that read from, or write to, stdin, stdout, and stderr. Most commands:

  • Read their input from the standard input stream (stdin, or file descriptor 0), which is normally assigned to your keyboard
  • Write their output to the standard output file (stdout, or fd 1), which is normally assigned to your display screen
  • Write any error messages to the standard error stream (stderr, or fd 2), which is also normally assigned to the screen

Sometimes you want to override this behavior.

If you want a process to: Use this symbol:
Read from a file, or another device (input redirection) <
Write stdout to a file (output redirection) >
Write stdout to a file, appending to the file's contents (output append) >>

For example, the ls command lists the files in a directory. If you want to redirect to output of ls to a file called filelist, enter:

ls > filelist

You can specify a file descriptor for the above redirections. For example, if you don't want to display any error messages, redirect stderr to dev/null (a special file, also known as the bit bucket, that swallows any data written to it and returns end-of-file when read from):

my_command 2> /dev/null

For more information, see Input/output redirection in the entry for ksh in Utilities.


You can use a pipe (|) to to build complex commands from smaller ones. For example:

grep 'some term' *.html | sort -u | wc -l

Programs such as grep , sort , and wc (a utility that counts characters, words, and lines) that read from standard input and write to standard output are called filters.

Quoting special characters

Certain characters may have special meaning to the shell, depending on their context. If you want a command line to include any of the special characters that the shell processes, then you may have to quote these characters to force the shell to treat them as simple characters. You must quote the following characters to avoid their special interpretation:

| $ ( " ) & ` ; \ ' Tab Newline Space

You might need to quote the following characters, depending on their context within a shell command:

* ? [ # ~ = %

To quote: You can:
A single character Precede the character with a single backslash (\) character
All special characters within a string of characters Enclose the whole string in single quotes
All special characters within a string, except for $, `, and \ Enclose the whole string in double quotes

For example, these commands search for all occurrences of the string realtime OS in the chapter1.html file:

grep realtime\ OS chapter1.html
grep 'realtime OS' chapter1.html
grep "realtime OS" chapter1.html

However, note that:

grep realtime OS chapter1.html

doesn't do what you might expect, as it attempts to find the string realtime in the files named OS and chapter1.html.

Depending on the complexity of a command, you might have to nest the quoting. For example:

find -name "*.html" | xargs grep -l '"realtime.*OS"' | less

This command lists all the HTML files that contain a string consisting of realtime, followed by any characters, followed by OS. The command line uses find to locate all of the files with an extension of html and passes the list of files to the xargs command, which executes the given grep command on each file in turn. All of the output from xargs is then passed to less , which displays the output, one screenful at a time.

This command uses quoting in various ways to control when the special characters are processed, and by which process:

  • If you don't put quotes around the *.html, the shell interprets the *, and passes to find the list of files in the current directory with an extension of html. If you quote the *.html, the shell passes the string as-is to find, which then uses it to match all of the files in this directory and below in the file system hierarchy with that extension.
  • In a similar way, if you don't quote the realtime.*OS string at all, the shell generates a list of files that match the pattern. Quoting it once ("realtime.*OS") works for a single invocation of grep, but this example has the added complexity of the xargs command.
  • The xargs command takes a command line as its argument, and the shell interprets this command line for each item that's passed to xargs. If you don't want the realtime.*OS string to be interpreted by the shell at all, you need to put nested quotes around the pattern that you want to pass to grep:
    xargs grep -l '"realtime.*OS"'
  • The quoting also indicates when you want to execute the less command. As given, the shell passes the output from all of the invocations of xargs to less. In contrast, this command:
    find -name "*.html" | xargs 'grep -l "realtime.*OS" | less'

    passes the command:

    grep -l "realtime.*OS" | less

    to xargs, which will have quite different results—if it works at all.

For more information, see Quoting in the entry for ksh in Utilities.

History: recalling commands

The shell lets you recall commands that you previously entered; use the up and down arrows to move through the history buffer. You can edit the command, if you want, and then press Enter to reexecute it. The shell also includes a built in fc command that you can use to display and edit previous commands, as well as an r alias to fc that reexecutes a previous command. For example:

r string

reexecutes the last command that starts with the given string.

Shell scripts

You can enter shell commands into a text file, called a shell script, and then invoke the commands in batch mode by executing (or shelling) the file. For more information, see Writing Shell Scripts.

Last modified: 2014-11-17

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus