Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
matthew@seymour:~$  env
...
HOSTTYPE=i386
HOSTNAME=thinkpad.home.org

Although the behavior of a shebang line is not defined by POSIX, variations
of its use can prove helpful when you are writing shell scripts. For example,
as described in the wish man page, you can use a shell to help execute
programs called within a shell script without needing to hard code pathnames
of programs. The wish command is a windowing Tool Control Language
(tcl) interpreter that can be used to write graphical clients. Avoiding the use
of specific pathnames to programs increases shell script portability because
not every UNIX or Linux system has programs in the same location.


For example, if you want to use the wish command, your first inclination
might be to write this:


Click here to view code image
#!/usr/local/bin/wish


Although this works on many other operating systems, the script fails under
Linux because wish is located in the /usr/bin directory. However, if you
write the command line this way, you can use the wish command (as a
binary or a shell script itself):


Click here to view code image
#!/bin/sh
exec wish "$@"


Using Variables in Shell Scripts


When writing shell scripts for Linux, you work with three types of variables:


Environment variables—You   can use these   variables,  which   are part    of
the system environment, in your shell program. You can define new
variables, and you can also modify some of them, such as PATH, within
a shell program.
Built-in variables—Variables such as options used on the command
(interpreted by the shell as a positional argument) are provided by Linux.
Unlike environment variables, you cannot modify built-in variables.
User variables—These variables are defined within a script when you
write a shell script. You can use and modify them at will within the shell
script, but they are not available to be used outside the script.

A major difference between shell programming and other programming
languages is that in shell programming, variables are not typed—that is, you

Free download pdf