Bash Shell ScriptCreating a simple shell script
How to create a script file:
#!/bin/bash
Displaying Messages echo command can display simple text string
By default, there is no need to use quotes to delineate the
string, but can get tricky when using quotes within
the string
There is a problem. It ignores the apostrophes and prints.If we want to define exactly what to print we use either double (") or single (') quotes to enclose text strings. Use one type within the text and the other
type to delineate the string
Double quotes allow variable substitution, single quotes do not. How to run a shell script – Method 1 Make the file executable After you create a shell script using a editor, make it executable by using chmod command
How to run a shell script – Method 2 Pass the shell script as a parameter to a shell program Run a shell script which does not have execution permission $bash filename Run the script with different shell other than your interactive shell $sh filename Environment Variables Shell maintains environment variables that track specific system information $ set
Reference environment variables by using the variable’s name preceded by a dollar sign echo $HOME Environment variable - PATH Another important environment variable is PATH. PATH is a list of directories that the shell uses to locate executable files for commands. So if the PATH is set to: /bin:/usr/bin:/usr/local/bin:. When you typed ls, the shell would look for /bin/ls, /usr/bin/ls etc. Note that the PATH contains '.', i.e. the current working directory. This allows you to create a shell script or program
and run it as a command from your current
directory without having to explicitly say Note that PATH has nothing to do with filenames that are specified as arguments to commands User variables Names
Assigning variables Values are assigned to the user variable using an equal sign. (NB: No spaces can appear between the variable, the equal sign and the value). User variables can be referenced using the $ sign:
An Example
When referencing a variable value, use the $ sign; when referencing the variable to assign a value to it, not to use the $ sign Variables created within a shell are local to that shell, so only that shell can access them. By default, variables are local To turn a local variable to an environment variables $export variable_name Environment variables are passed to subprocesses. Local variables are not. The backtick Backtick (`) is the lowly back quote character. The backtick allows you to assign the output of a shell command to a variable.
Positional Parameters The command name and arguments can by referenced by their position on the command line $0 : Name of the calling program $1 - $9 : Command-line Arguments The first argument is represented by $1 The second argument is represented by $2 $./display_args 1 2 3 4 5 you are running script ./display_args with Access the parameter after $9 use ${10}, ${11}, etc. shift Built-in command shift promotes each of the command-line arguments. The first argument (which was $1) is discarded The second argument (which was $2) becomes $1 The third becomes the second and so on. Repeatedly using shift is another way to loop over all the command-line arguments Positional Parameters Special Parameters Special Parameters $? |
|