Table of Contents for
Learning Linux Shell Scripting

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Learning Linux Shell Scripting by Ganesh Sanjiv Naik Published by Packt Publishing, 2015
  1. Cover
  2. Table of Contents
  3. Learning Linux Shell Scripting
  4. Learning Linux Shell Scripting
  5. Credits
  6. About the Author
  7. Acknowledgments
  8. About the Reviewers
  9. www.PacktPub.com
  10. Preface
  11. What you need for this book
  12. Who this book is for
  13. Conventions
  14. Reader feedback
  15. Customer support
  16. 1. Getting Started and Working with Shell Scripting
  17. Tasks done by shell
  18. Working in shell
  19. Learning basic Linux commands
  20. Our first script – Hello World
  21. Compiler and interpreter – difference in process
  22. When not to use scripts
  23. Various directories
  24. Working more effectively with shell – basic commands
  25. Working with permissions
  26. Summary
  27. 2. Drilling Deep into Process Management, Job Control, and Automation
  28. Monitoring processes using ps
  29. Process management
  30. Process monitoring tools – top, iostat, and vmstat
  31. Understanding "at"
  32. Understanding "crontab"
  33. Summary
  34. 3. Using Text Processing and Filters in Your Scripts
  35. IO redirection
  36. Pattern matching with the vi editor
  37. Pattern searching using grep
  38. Summary
  39. 4. Working with Commands
  40. Command substitution
  41. Command separators
  42. Logical operators
  43. Pipes
  44. Summary
  45. 5. Exploring Expressions and Variables
  46. Working with environment variables
  47. Working with read-only variables
  48. Working with command line arguments (special variables, set and shift, getopt)
  49. Understanding getopts
  50. Understanding default parameters
  51. Working with arrays
  52. Summary
  53. 6. Neat Tricks with Shell Scripting
  54. The here document and the << operator
  55. The here string and the <<< operator
  56. File handling
  57. Debugging
  58. Summary
  59. 7. Performing Arithmetic Operations in Shell Scripts
  60. Using the let command for arithmetic
  61. Using the expr command for arithmetic
  62. Binary, octal, and hex arithmetic operations
  63. A floating-point arithmetic
  64. Summary
  65. 8. Automating Decision Making in Scripts
  66. Understanding the test command
  67. Conditional constructs – if else
  68. Switching case
  69. Implementing simple menus with select
  70. Looping with the for command
  71. Exiting from the current loop iteration with the continue command
  72. Exiting from a loop with a break
  73. Working with the do while loop
  74. Using until
  75. Piping the output of a loop to a Linux command
  76. Running loops in the background
  77. The IFS and loops
  78. Summary
  79. 9. Working with Functions
  80. Passing arguments or parameters to functions
  81. Sharing the data by many functions
  82. Declaring local variables in functions
  83. Returning information from functions
  84. Running functions in the background
  85. Creating a library of functions
  86. Summary
  87. 10. Using Advanced Functionality in Scripts
  88. Using the trap command
  89. Ignoring signals
  90. Using traps in function
  91. Running scripts or processes even if the user logs out
  92. Creating dialog boxes with the dialog utility
  93. Summary
  94. 11. System Startup and Customizing a Linux System
  95. User initialization scripts
  96. Summary
  97. 12. Pattern Matching and Regular Expressions with sed and awk
  98. sed – noninteractive stream editor
  99. Using awk
  100. Summary
  101. Index

Working with environment variables

Environmental variables are inherited by any subshells or child processes. For example, HOME, PATH. Every shell terminal has the memory area called environment. Shell keeps all details and settings in the environment. When we start a new terminal or shell, this environment is created every time.

We can view environment variables by the following command:

$ env

Or:

$ printenv

Output:

Working with environment variables

This is the output of the $ env command. The list of environment variables will be quite extensive. I advise you to browse through the complete list. We can change the content of any of these environment variables.

Environmental variables are defined in a terminal or shell. They will be available in subshells or child shells created from the current shell terminal. You will learn about these activities in the next few sections. You have already learned that every command in shell creates a new subshell from the current shell.

The following is a brief summary of the few environmental variables:

Working with environment variables

Whenever any user logs in, the /etc/profile Shell script is executed.

For every user, the .bash_profile Shell script is stored in the home folder. The complete path or location is /home/user_name/.profile.

Whenever a new terminal is created, every new terminal will execute the script .bashrc, which is located in the home folder of every user.

The local variable and its scope

In the current shell, we can create and store user defined variables. These may contain characters, digits, and "_". A variable should not start with a digit. Normally for environment variables, upper case characters are used.

If we create a new variable, it will not be available in subshells. The newly created variable will be available only in the current shell. If we run Shell script, then local variables will not be available in the commands called by Shell script. Shell has one special variable $$. This variable contains the process ID of the current shell.

Let's try a few commands:

$ echo $$
1234

This is the process ID of the current shell.

$ name="Ganesh Naik"
$ echo $name
Ganesh Naik

We declared the variable name and initialized it.

$ bash

This command will create a new subshell.

$ echo $$
1678

This is the process ID of the newly created subshell.

$ echo $name

Nothing will be displayed, as the local variables from the parent shell are not inherited in the newly created child shell or subshell:

$ exit

We will exit the subshell and return to the original shell terminal.

$ echo $$
1234

This is the process ID of the current shell or parent shell.

$ echo $name
Ganesh Naik

This is displaying the variable's presence in the original shell or parent shell.

Variables created in the current shell will not be available in a subshell or child shell. If we need to use a variable in a child shell, then we need to export them using the export command.

Exporting variables

Using the export command, we are making variables available in the child process or subshell. But if we declare new variables in the child process and export it in the child process, the variable will not be available in parent process. The parent process can export variables to child, but the child process cannot export variables to the parent process.

Whenever we create a Shell script and execute it, a new shell process is created and the Shell script runs in that process. Any exported variable values are available to the new shell or to any subprocess.

We can export any variable as follows:

$ export NAME

Or:

$ declare -x NAME

Let's understand the concept of exporting the variable by the following example:

$ PERSON="Ganesh Naik"
$ export PERSON
$ echo $PERSON
Ganesh Naik
$ echo $$
515

The process ID of the current shell or parent shell is 515.

$ bash

This will start a subshell.

$ echo $$
555

This is the process ID of new or subshell.

$ echo $PERSON
Ganesh Naik
$ PERSON="Author"
$ echo $PERSON
Author
$ exit

This will terminate the subshell, and will be placed in the parent shell.

$ echo $$
515

This is the process ID of the parent shell.

$ echo $PERSON
Author

Let's write Shell script to use the concept we learned:

# Ubuntu Timezone files location : /usr/share/zoneinfo/
# redhat "/etc/localtime"  instead of "/etc/timezone"
# In Redhat
# ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

export TZ=America/Los_Angeles
echo "Your Timezone is = $TZ"
date
export TZ=Asia/Tokyo
echo "Your Timezone is = $TZ"
date

unset TZ

echo "Your Timezone is = $(cat /etc/timezone)"
# For Redhat or Fedora /etc/localtime
date
Exporting variables

The date command checks the TZ environmental variable. We initialized the TZ for Los_Angeles, then to Tokyo, and then finally we removed it. We can see the difference in the date command output.

Let's write another Shell script to study the parent and child process and exportation of variables.

Create the export1.sh Shell script:

#!/bin/bash
foo="The first variable foo"
export bar="The second variable bar"
./export2.sh

Create another shell script export2.sh
#!/bin/bash
echo "$foo"
echo "$bar"
Exporting variables

The Shell script export1.sh runs as a parent process and export2.sh is started as a child process of export1.sh. We can clearly observe that the variable bar, which was exported, is available in the child process; but variable foo, which was not exported, is not available in the child process.