Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
Home Bash Guide for Beginners Bash and Bash scripts Shell building blocks | ||
See also: Common shell programs, Shell Expansion | ||
Search the VIAS Library | Index | ||
Shell Building BlocksShell syntax
Shell commandsA simple shell command such as touch file1 file2 file3 consists of the command itself followed by arguments, separated by spaces. More complex shell commands are composed of simple commands arranged together in a variety of ways: in a pipeline in which the output of one command becomes the input of a second, in a loop or conditional construct, or in some other grouping. A couple of examples: ls | more gunzip file.tar.gz | tar xvf - Shell functionsShell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Shell functions are executed in the current shell context; no new process is created to interpret them. Functions are explained in Chapter 11. Shell parametersA parameter is an entity that stores values. It can be a name, a number or a special value. For the shell's purpose, a variable is a parameter that stores a name. A variable has a value and zero or more attributes. Variables are created with the declare shell built-in command. If no value is given, a variable is assigned the null string. Variables can only be removed with the unset built-in. Assigning variables is discussed in Section 3.2, advanced use of variables in Chapter 10. Shell expansionsShell expansion is performed after each command line has been split into tokens. These are the expansions performed:
We'll discuss these expansion types in detail in Section 3.4. RedirectionsBefore a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment. Executing commandsWhen executing a command, the words that the parser has marked as variable assignments (preceding the command name) and redirections are saved for later reference. Words that are not variable assignments or redirections are expanded; the first remaining word after expansion is taken to be the name of the command and the rest are arguments to that command. Then redirections are performed, then strings assigned to variables are expanded. If no command name results, variables will affect the current shell environment. An important part of the tasks of the shell is to search for commands. Bash does this as follows:
|
||
Home Bash Guide for Beginners Bash and Bash scripts Shell building blocks |