Sunday, October 25, 2009

Command-Line Syntax









Command-Line Syntax


Type commands on the command line, following the command prompt. When you press <Enter> after typing the command, the command is executed. While the command is executing, no system prompt displays. When the command finishes running, the system prompt displays, indicating that the shell is ready to accept another command.


Tip



While a command is executing, you can stop it by pressing <Ctrl+c>.



A command consists of the command, options, and arguments in the following format:



commandname options arguments


where


  • Commandname is the name of the command.

  • Options are keywords that affect command execution. Options consist of one character, preceded by a hyphen (-), or a character string, preceded by two hyphens (--). The hyphens tell the shell that the next characters are an option. For most commands, options are optional.

  • Arguments are information that the command uses during execution. For instance, a command may process a file, so that you need to pass the name of the file as an argument. You can pass as many arguments as the command needs. Not all commands require arguments. Some commands that require arguments can use default values when you don't pass the arguments. If an argument includes spaces or special characters, enclose it with quotes on the command line, so that the shell knows it's one argument, rather than two.


The following command shows a command name, an option, and an argument:



ls -l /home/janet


Thels command lists the files in a directory. The argument is a path to a directory; the files in the directory specified by the argument (janet) are displayed on the screen. However, ls is a command that doesn't require an argument, using a default when you don't pass an argument. If you don't include a directory name, the ls command lists the files in the current directory.


The -l is one of many options you can use with the ls command to determine the information displayed and its format. The -l option produces output with more information than the standard ls output. The output from the standard command, without the -l, is the following list of files in the directory /home/janet:



ls /home/janet
text.txt
text2.txt


while the output from the ls command with the -l is longer, as shown below:



ls l /home/janet
-rw-r--r-- 1 janet janet 50 Apr 25 11.09 test.txt
-rw-r--r-- 1 janet janet 52 Apr 25 11.15 test2.txt


You can enter more than one option as follows:



ls -ld /home/janet


You can enter more than one command, separated by a semicolon (;), as follows:



ls /home/janet; ls /home/kim


Tip



You can enter commands that are more than one line long. Type a backslash (\) at the end of the line, before pressing Return, and the second line will be treated as a continuation of the first line.




Creating Shortcuts for Long Commands


You might have a long command that you type often. If so, you can create a short alias for the long command, so that you don't have to type the long command every time you need it. You can type the short alias instead.


For instance, the following command creates the alias listallan alias for the four ls commands:



alias listall='ls /home/janet; ls /home/kim; ls /home/george; \

ls /home/sam'


You can now type listall to display the files in all four directories.










    No comments: