LINUX | Files | Directories | Programs | Expressions | Utilities | Shell Scripts
Shell scripts are program files built from programming languages local to the shell. The syntax varies with each shell. The bash shell is used in these tutorials. Scripts can be coded in a separate file or directly in the shell. To use the bash shell while in another shell, enter the #! operator and the absolute path to the bash program...
The # pound operator declares a user comment when used elsewhere in a bash script.
The vi program can be used to create a script file that stores command statements. A command statement is one or more commands executed as a block. Scripts are interpreted rather than compiled. To start scripting, enter the vi command, followed by the name of the file as the argument, then hit enter...
Go to insert mode (i) and type commands each on a separate line...
Go to command mode (Esc), then write and quit the file (:wq). The file can be called up with the bash command. The commands in fileName1 are then executed...
The script file must be executable to run. If necessary, change the permissions of the file...
Commands can be coded directly in the shell. The echo command outputs values to the screen...
A variable, also known as a scalar, is a command that stores a single value. A variable can be be accessed anywhere in the script when needed. There are two types of shell variables. User variables are local and have a lowercase naming convention. Environmental variables are global and have an UPPERCASE naming convention. Both types contain read and read/write variables.
A variable can be created by the user, which is temporary unless stored in a script file. User variable names are created and case-sensitive, and can only include letters, numbers, and underscores. A number is not allowed as the first character. A variable is assigned a value with the = equal sign as the assignment operator, without spaces. By default, the value of a variable is a string. The quotes are required when using spaces or metacharacters. Quotes are not used when the value is a number. A number is automatically interpreted either as a string or numeric value, depending on the operation...
the $ dollar sign is only used when calling the variable, not when creating the variable...
A variable can store a command when the value is declared within `backquotes`. The command name is replaced by its output when the variable is called up...
Global environmental variables control interface settings. The following are common environmental variables...
$PATH - name of search path for commands
$SHELL - name of current shell
$USER - login name of user
$HOME - location of home directory
$HOST - name of computer
$PRINTER - name of default printer
The following are common local variables...
$history - number of commands last used
$pwd - present working directory
$home - the path name of the home directory
$path - the directories the shell searches to find a command
$prompt - the text string used to prompt for interactive shell
$commands - your login shell
The default setting of an environmental variable can output to the screen by adding the variable as an argument to the echo command...
The set command outputs a list of all variables to the screen...