330 likes | 499 Views
Workbook 6 – Part 1 The Bash Shell. RH030 Linux Computing Essentials. Workbook 6 Objectives. This workbook is all about the shell. Understanding shell functionality what a shell is and does Understanding what the term ‘subshells’ refers too.
E N D
Workbook 6 – Part 1 The Bash Shell RH030 Linux Computing Essentials
Workbook 6 Objectives • This workbook is all about the shell. • Understanding shell functionality • what a shell is and does • Understanding what the term ‘subshells’ refers too. • Overview of shell features Metacharachers, redirection, piping • History • shell variables • Shell variables • Local verse Environmental • Creating new variables • Exporting variables • Identifying common shell environment variables • $LOGNAME, $SHELL,$HOME, $PATH • Manipulating common shell environment variables • Startup & Login environment files – covered next week • global & local environment files. • Used to create permanent aliases/variables Linux+ Guide to Linux Certification, 2e
Chapter 1 – Introduction to Bash • Key Concepts • The default shell in Red Hat Enterprise Linux is the bash shell. • The bash shell can be used interactively, or as a powerful scripting language. • bash allows users to customize their shell & local environment. • All shells have functionality – such as history. • The bash shell maintains a history of the command lines that it executes. • Command lines can be retrieved from the history using various history expansions that begin with "!". Linux+ Guide to Linux Certification, 2e
Understanding the Shell • A shell is just a program: • This program acts as an interface between the user and the system. • It is run from an executable file. • The executable files for shells are stored in /bin • The /etc/shell configuration file determines what different shells’s are valid for this system. • How the shell is used: • Ashell can be used interactively, or as a powerful scripting language. • Interactively it: • Provides the user a command-line interface to the system. • Interpreting the commands as they are run. • Manipulating the commands input and output • BASH is the most commonly used. Linux+ Guide to Linux Certification, 2e
There are many different Shells available. • Different shells have different functionality. • Bourne /bin/sh • original shell program • developed by Stephen Bourne for AT&T • Very limited functionality • Korn /bin/ksh • superset of the Bourne shell • added features such as aliasing and history • once most widely used shell • Bash /bin/bash • superset of the Bourne & Korn shell • added features such as aliasing and history • Now most widely used shell • C Shell /bin/csh • based on the C programming language Linux+ Guide to Linux Certification, 2e
Interactively Processing commands • The shell interactively manages commands as they are run. • Interpreting the contents of the commands. • Manipulating the commands input and output • A command is really just a utility program which the shell runs. • When a program is run it becomes a process that the kernel carries out. • For every actions performed there are 2 possible results. 2> Linux+ Guide to Linux Certification, 2e
It uses the standard I/O devices on the system to handle the commands input/output. The three common file descriptors Linux+ Guide to Linux Certification, 2e
Shells allow us to use redirection • The shell provides a way to redirect flow of information to and from a command away from these standard devices. • It also allows you to separate the error outputs. Linux+ Guide to Linux Certification, 2e
Redirection can get very complicated. Linux+ Guide to Linux Certification, 2e
Shells allow us to use command piping • A pipe passes the output from one command to become the input of another command. Figure 8-2: Piping information from one command to another Linux+ Guide to Linux Certification, 2e
Shells allow us to use command piping Figure 8-2: Piping information from one command to another Linux+ Guide to Linux Certification, 2e
Piping can get very complicated. • Command piping passes the standard output of one command to the standard input of another. • Commonly used to reduce or filter the amount of information displayed on terminal screen • Example of pipe & redirection:ls –l/etc| sort +n2 | head 12 > savefile Linux+ Guide to Linux Certification, 2e
Using Shell HistoryFunction • As a convenience to users of interactive shells, the bash shell keeps a history of each command entered by the user, and provides a variety of ways to make commands from this history available at the finger tips. cat ~/.bash_history • The easiest way to view your current history is to use history command. Substitution • As an alternative to the arrow keys, the bash shell also performs "history substitution", which is triggered by the exclamation point. • !! Previous command • !n Command number • n !-n The nth most recent command • !cmd The most recent command that began cmd Linux+ Guide to Linux Certification, 2e
~/.bashrc • bash executes commands found in the ~/.bashrc file. • Used upon login and every time a new shell (subshell) is started. • Thus allowing users to customize their shell & local environment. • Use vi to modify ~/.bashrc # infront of the line = comment line date >> .bash_timestamps • Or use echo >> to append to the file echo umask 777 >> ~/.bashrc echo date >> .bash_timestamps >> ~./.bashrc Linux+ Guide to Linux Certification, 2e
Chapter 2 – command lists & scripts • These examples are being used to help develop your understanding of the use of subshells. • Example 1 - Multiple commands can be separated with a ; • Note - When using a “;” to run multiple commands the shell's current working directory is changed as the commands are run. [elvis@station elvis]$ cd /etc/X11; ls applnk prefdm sysconfig xorg.conf.backup xkb desktop-menus proxymngr twm xorg.conf.wbx Xmodmap fs rstart X xorg.conf.works Xresources gdm serverconfig xdm XftConfig.README-OBSOLETE xserver lbxproxy starthere xorg.conf xinit xsm [elvis@station X11]$ Linux+ Guide to Linux Certification, 2e
Running commands in a sub-shell • Here they use a sub-shell in which to run the commands • When using “parenthesis” to run multiple commands the shell's current working directory is left unchanged. • When bash encounters parenthesis on the command line, it spawns an entirely new child bash process (called a subshell). • And runs the commands within the subshell. • After running the commands, the subshell exits, BUT the user is left in the original (unchanged) shell. • Example 2 - commands are wrapped in parenthesis [elvis@station elvis]$ (cd /etc/X11; ls) applnk prefdm sysconfig xorg.conf.backup xkb desktop-menus proxymngr twm xorg.conf.wbx Xmodmap fs rstart X xorg.conf.works Xresources gdm serverconfig xdm XftConfig.README-OBSOLETE xserver lbxproxy starthere xorg.conf xinit xsm [elvis@station elvis]$ Linux+ Guide to Linux Certification, 2e
Upon exiting, every command returns an integer to its parent called a return value. • What is a return value • It is to tell the system whether the command worked or failed. • Held in the shell variable $? • This can be used to display the returned value of previously executed command. • This example shows how the ls command succeeded. [elvis@station elvis]$ ls -l /etc/passwd -rw-r--r-- 1 root root 3694 Aug 15 16:26 /etc/passwd [elvis@station elvis]$ echo $? 0 • In contrast, the following example shows how the ls command failed [elvis@station elvis]$ ls -l /etc/password ls: /etc/password: No such file or directory [elvis@station elvis]$ echo $? 1 echo Linux+ Guide to Linux Certification, 2e
Running Multiple Command Conditionally • The bash shell uses && and || to join two commands conditionally. • When commands are conditionally joined, the first will always execute. • The second command may execute or not. • Depending on the return value of the first command. For example • By coupling two commands with &&, the second command will only run if the first command succeeded • (i.e., had a return value of 0). [elvis@station elvis]$ mkdir /tmp/boring && mv numbers.txt /tmp/boring [elvis@station elvis]$ ls Linux+ Guide to Linux Certification, 2e
Difference of && or ||& • Wth ||bash will execute the second command only if the first command "fails" • (i.e., had a return value of 1). [elvis@station elvis]$ mkdir /tmp/boring || mv numbers.txt /tmp/boring [elvis@station elvis]$ ls • In the above example, if the mkdir command succeeded, • Will the file be moved? Linux+ Guide to Linux Certification, 2e
Chapter 3 – Bash Variables • Key Concepts • Shell variables are assigned using an A=apple syntax. • Variables are examined with the $ character, as in echo $A. • At the kernel level, every process (such as a subshell) has a collection of environment variables, which are inherited by child processes. • The export command converts a shell variable into an environment variable. • The set and env commands list shell variables and environment variables, respectively. Linux+ Guide to Linux Certification, 2e
What is a variable? • A variable is a placeholder for information required by a program or processes so that it can function properly. • A variable has a name which holds a value. <variable-name>=value sheila=teacher • It contains accessible information that can be changed. • Changing the value of a variable is called setting the variable. • Displaying the value of a variable: • To view contents of a specified variable you must always use a $ sign infront of the variable name. • This tells the shell you are referring to the value of the variable • NOT the variable itself. echo $sheila Linux+ Guide to Linux Certification, 2e
The shell manages the variables There are 2 types of variables: • User-defined variables: • Commonly called local variables • Created and defined by the local user • A user can create their own local variables in any shell. • Environment variables: • Contains system information or properties which system needs to run. • These are used by the system and programs in the running of the overall of the system and accessed regularly. • Such as required for each user profile. • Such as your unique login id. Linux+ Guide to Linux Certification, 2e
Creating a new local variables • Creating or Changing value of a variable: • To create a new variable name you use the following format. • <new-variable-name> equal sign (=) and new value Sheila=teacher • Double quotes should surround an entire string. Sheila= “Sheila is the teacher” • To display the value of any variable using the echo command. echo $Sheila teacher Linux+ Guide to Linux Certification, 2e
All operating systems use variables • Configuration scripts use environmental variables • They are commonly used to store and refer to any changeable information within system configuration files or scripts . • Setting or modifying information used in the multiuser environment • They allow the administrator to change the information being used within the script without having to actually edit the system configuration files or script. • As it is the value of the variable which is being used the in system configuration files and scripts that changes. • The variable name never changes and it is this name which is written into system configuration files and scripts. $ USER=Sheila $ echo “Welcome to the system $USER” $ Welcome to the system Sheila Linux+ Guide to Linux Certification, 2e
Environment Variables • Most variables available in the system are global environment variables. • They are used to allow a networking or multiuser environment. • They are commonly referred to as system wide or global variables • These are automatically setup for each user as you login. • They are read from global or system wide environment startup files • /etc/profile /etc/bashrc • And then from the local shell’s environment startup files. • ~/.bash_profile ~/.bashrc • Each user can often modify these values to suit their specific needs in their own local environment Linux+ Guide to Linux Certification, 2e
Examples of Some Frequently Used Environmental Variables Name Description • USER Sets the unique id by which this user will be identified • HOME Sets the directory which will be this users home directory. • PATH Specifies the directories that the shell is to look through to find an executable command. These directories are searched in the order of the path. • SHELL Sets your default shell. • HOSTNAME Sets the name of your system • MAIL Location of the users mailbox. • PS1 Sets how your prompt will be displayed. Linux+ Guide to Linux Certification, 2e
Displaying Variables & their values • Displaying all currently available variables & their current values setcommand: • Displaying the value of a specific variable: • To view contents of a specified variable you must always use a $ sign infront of the variable name. • This tells the shell you are referring to the value of the variable • NOT the variable itself. echo $HOME Linux+ Guide to Linux Certification, 2e
Examples - Environment Variables Table 8-3: Common BASH environment variables Linux+ Guide to Linux Certification, 2e
Examples - Environment Variables Table 8-3 (continued): Common BASH environment variables Linux+ Guide to Linux Certification, 2e
Local Variables User-defined variables • A user can create their own local variables in any shell. • But it will only be available in the current shell. Subshell = A new shell opened by another shell • Most commands run in a subshell. • Variables created in the current shell are not automatically available to it’s subshells Unless you export them. • This make them available to any new subshells They will still only be temporary • To make them available permanently you will need to put them into your shell’s local script startup environment files. • More on this next week. Linux+ Guide to Linux Certification, 2e
Exporting Variables • Changes how the system sees them. • It makes them into an environmental variable • And the entire system becomes aware of them. • For a variable to be automatically available to all subshells it needs to be exported or declared to the entire system that they are available for use. • The export command Is used to do this. • It declares the existence of defined variables within the system. • This makes them available to all subshells. export • lists all the currently exported / declared available variables export teacher • Used with an existing variable it will export/declare that variable. Linux+ Guide to Linux Certification, 2e
Modifying the values of variables $PATH Environment Variable • Displays the current settings on the system path. echo $PATH • Backup the current path settings into a new variable OLDPATH=$PATH • Add your new directory ensuring you don’t overwrite the existing settings already being used on the system path. PATH=$PATH: newpathname to add Linux+ Guide to Linux Certification, 2e
Summary • bash • echo $? • && || • set env export • $ PATH $HOME $SHELL • /etc/profile • /etc/bashrc • ~/.bash_profile • ~/.bashrc Linux+ Guide to Linux Certification, 2e