1 / 33

Workbook 6 – Part 2 The Bash Shell

Workbook 6 – Part 2 The Bash Shell. RH030 Linux Computing Essentials. Workbook 6 Objectives. This workbook is all about the shell. Last week we discussed What is a Shell What a shell is and what it does How subshells is and how it works Overviewed shell features Shell variables

john-curtis
Download Presentation

Workbook 6 – Part 2 The Bash Shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Workbook 6 – Part 2 The Bash Shell RH030 Linux Computing Essentials

  2. Workbook 6 Objectives • This workbook is all about the shell. Last week we discussed • What is a Shell • What a shell is and what it does • How subshells is and how it works • Overviewed shell features • Shell variables • Should understand difference of Local verse Environmental variables • Creating new shell variables • Exporting variables • Identifying common shell environment variables • $LOGNAME, $SHELL,$HOME, $PATH • Manipulating common shell environment variables • ~/.bashrc Linux+ Guide to Linux Certification, 2e

  3. Workbook 6 Objectives This week • Shell feature = Command Line expansion • Startup & Login environment files. • global & local environment files. • Sequence of the startup environmental files • Use of each startup environmental file • Manipulating common shell environment variables permanently • Creating permanent aliases/variables Linux+ Guide to Linux Certification, 2e

  4. Chapter 4 – Command line expansion • Key Concepts • The bash shell expands certain command line metacharacters before interpreting the command. • Tilde expansion expands tokens that begin with a tilde (~) to users home directories. • Brace expansion expands tokens with braces ({}) into multiple words, each of which contains a single word from the specified list. • Command substitution expands text enclosed within backticks (``) or "dollar parenthesis" ($()) into the output produced by the enclosed command. • Double quotes ("..." ), single quotes ('...'), and the backslash character can be used to protect characters from being expanded by the shell. Linux+ Guide to Linux Certification, 2e

  5. Using Command Line Expansion • Expansion Area Syntax Expands To History ! Refer to a previous command line Brace {} Create this specified text Tilde ~ Refer’s to a user's home directory Variable $, ${...} Shell and environment variables Arithmetic $((...)) Numeric calculation Command Substitution ` ...`, Allows command to run in subshell Pathname *, ?, [^...] Matchs to filenames in filesystem Linux+ Guide to Linux Certification, 2e

  6. Brace • Quick way to create items Create this specified text touch file{01,02,03,04} • [student@station student]$ ls file01 file02 file03 file04 • Creating a hierarchy structure [student@station student]$ mkdir chap{01,02,03,04}/{html,text} Linux+ Guide to Linux Certification, 2e

  7. It can get quite complicated • $touch soap/episode{1,2,3,4}.{draft,final}.{txt,pdf} • Will produce the following directory listing. • [student@station student]$ ls soap/episode1 episode2.draft.pdf episode3.draft.pdf episode4.draft.pdfepisode1.draft.txt episode2.draft.txt episode3.draft.txt episode4.draft.txt episode1.final.pdf episode2.final.pdf episode3.final.pdf episode4.final.pdf episode1.final.txt episode2.final.txt episode3.final.txt episode4.final.txt Linux+ Guide to Linux Certification, 2e

  8. Pathname Expansion • Character Matching * 0 or more characters ? exactly one characters [...] exactly one of the included characters [^...] exactly one of the excluded characters Linux+ Guide to Linux Certification, 2e

  9. Different Quotes do different things Linux+ Guide to Linux Certification, 2e

  10. Using quota’s • Sometime the entire line needs to be enclosed in quota’s • Double quotes tell the shell to: ( “ ) • Display the value of this variable used. LOGIN=John echo “Welcome $LOGIN” Welcome John • Single quotes tell the shell to: ( ‘ ) • Display the name of this variable used LOGIN=John echo ‘Welcome $LOGIN’ Welcome $LOGIN • Back quotes ( ` ) tell the shell to run text this as a command echo “ Welcome $LOGIN today is `date` ” Linux+ Guide to Linux Certification, 2e

  11. Command Substitution • The "old school" syntax for command substitution is to encapsulate the command in `back ticks` • The more modern syntax supported by the bash shell is similar to arithmetic expansion, but with only one set of parentheses: $(subcommand) • [prince@station prince]$ mkdir reports.$(date +%d%b%Y) Linux+ Guide to Linux Certification, 2e

  12. Quoting • Put example of quotes from 1st ppt here Linux+ Guide to Linux Certification, 2e

  13. Using quota’s • Here with no quotes, bash interprets the > and < characters as requests to redirect the command's output (and input). $ echo <pre> little red $CAR </pre> $ -bash: syntax error near unexpected token `newline' • Here the double quotes protected the < and > characters. • The dollar sign, however, is interpreted as a marker for a variable. $ echo “<pre> little red $CAR <pre>“ $ <pre>little red corvette </pre> • Here the single quotes stop the variable expansion $ echo '<pre>little red $CAR</pre>' $ <pre>little red $CAR</pre> Linux+ Guide to Linux Certification, 2e

  14. Escape Characters • Shell expansions occur before the command is run. • Occasionally, some commands use arguments that contain characters in them that are special to the bash shell. • Meaning that for bash they have some other meaning. • Bash needs to be told NOT to read them as special characters. but to use them in the current syntax. \<special character> • A common example is the find command. find –name “*.chap” –exec rm {} \; Linux+ Guide to Linux Certification, 2e

  15. Use of escape character • echo \<pre\>little red $CAR\</pre\> <pre>little red corvette</pre> • echo \<pre\>little red \$CAR\</pre\> <pre>little red $CAR</pre> Linux+ Guide to Linux Certification, 2e

  16. Chapter 5 – Shell Customizations Key Concepts • The bash shell internally implements certain simple commands (internal Commands) • Which affect the shell's behavior. • These are referred to as builtin commands. • Shell aliases are established and examined with the alias command. • Shell aliases are removed with the unalias command. • The bash shell prompt can be customized using the PS1 variable. • Shell flags can can be set with the set -f command, and cleared with set +f. • Shell options are examined, set, and unset using the shopt command. Linux+ Guide to Linux Certification, 2e

  17. Using alias’s • alias command: • Used to list all the available aliases. • Using aliases • Short alias runs a long command-line • Can create a single alias for multiple commands • But are only temporarily available unless you make them permanent. • Locally this is done using the .bashrc environment file. • Creating temporary alias’s • alias c=clear • alias c=“ clear;date;ls –l ” • alias ts="touch $(date +timestamp.%H:%M:%S)" • alias tsc="find . -newer" Linux+ Guide to Linux Certification, 2e

  18. ~/.bashrc = local permanent aliases • Making aliases permanent • echo alias c=clear >> .bashrc • . ./.bashrc Linux+ Guide to Linux Certification, 2e

  19. Remember - Last week we looked at variables There are 2 types of variables: • User-defined variables: • Custom variables • Created and defined by the local user • Environment variables: • Contain system information or properties that the system and programs both use in the running of the overall of the system and access regularly. • Such as your unique login id. Linux+ Guide to Linux Certification, 2e

  20. Examples of Some Frequently Used Environmental Variables • Name Description • LOGNAME 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

  21. Working with Variables & their values • Displaying all currently available variables & their current values set • 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. • echo $HOME • 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” Linux+ Guide to Linux Certification, 2e

  22. Modifying existing variables • Customizing Your Prompt echo $PS1 “[\u@\h \W]$” Default setting PS1=“Hello World” echo $PS1 Hello World /home/sheila $pwd /home/sheila $ /home/sheila$ PS1= “This month is `date +%h ` @” This month is May @ The back quotes (`) are used to run it as a command instead of interpreting it literally as a string of text. Linux+ Guide to Linux Certification, 2e

  23. Linux+ Guide to Linux Certification, 2e

  24. set verse shopt • These two builtin commands are used to configure the shell's behavior using shell options. • In other words they turn on functions. • One is the set command, which can also be used to modify the shell's behavior using (usually) single letter flags set –f set –o noclobber (c shell function) • The other is shopt, which is used to configure shell options. shopt –s cdspell shopt –u cdspell Linux+ Guide to Linux Certification, 2e

  25. Chapter 6 – Sourcing Shell Scripts and Shell Initialization Key Concepts • Shell scripts are sourced with the source or . commands. • Shell scripts are all executed in the same shell that sources the script. • Bash shells can be either login or non-login shells. • Bash shells can be either interactive or non-interactive shells. • /etc/profile, files within the /etc/profile.d directory, and ~/.bash_profile are sourced on startup of every login shell. • /etc/bashrc and ~/.bashrc are sourced on startup of every shell. • ~/.bash_logout is sourced by every exiting login shell. Linux+ Guide to Linux Certification, 2e

  26. source = run this script • [blondie@station blondie]$ cat prompts.script PS1="whadda you want? " #PS1="\a\u@\H \$(date --iso-8601) \t [\!] \$ " #PS1="[\u@\h \W]\$ " • In order to try out the first prompt, she sources the file prompts.script. [blondie@station blondie]$ source prompts.script whadda you want? • Can also use . ./prompts.script • Can also use bash ./prompts.script Linux+ Guide to Linux Certification, 2e

  27. Environment Files • Environment files are used as part of the logon process • During login all users are assigned many default values. • Such as a default shell, path, home directory etc • All the different shells each use their own environment files. • When their shell is started it reads it’s own environment files. • There are 2 types of environment files. • Global –used when you login and given to everyone • Local – Also read when you login PLUS when you start a new subshell –used only to the local specific user. Linux+ Guide to Linux Certification, 2e

  28. Login Process • Environment files are just : • Script configuration files • Some are run everytime you login. • There are 2 types: • Global environment files • /etc/profile • /etc/bashrc • local • Bash environment Files • ~/.bash_profile • ~/.bashrc • ~/.bash_logout Linux+ Guide to Linux Certification, 2e

  29. Last week we discussed exporting variables • Exporting variables = Making the entire system aware of them. • It declares the existence of defined variables within the system. • A variable needs to be exported or declared to the entire system so that they are available for use. • Making them available to all subshells. export command = 2 uses • Used on it’s own • It will lists all the currently exported / declared available variables export teacher • Used with an existing variable it will export/declare that variable. • This then ensures that all sub-shells have access to this variable Linux+ Guide to Linux Certification, 2e

  30. Items - you would export in Global Environment • Exports the login id you used to connect to the system with. • $LOGNAME for login name etc • Exports PATH for Default Command Path • $PATH sets default path of directories where the shell will look when a command is executed • Exports MAIL for Default Mailbox location • $MAIL sets default path of directories containing each specific users mailbox location. • Sets TERM Variable Default Terminal Type • screen and keyboard • Displays Contents of /etc/motd File • Holds any motd is ‘message of the day’ • Sets Default File Creation Permissions • sets umask values which determines the default permissions • Checks for Mail • Checks mail and will print a mail message upon login Linux+ Guide to Linux Certification, 2e

  31. Order of the environment files • /etc/profile is read first • It contains the global variables. • It exports the global variables. • ~/.bash_profile is read next • It contains the local variables. • It exports the local variables. • ~/.bashrcis read next • It contains the local aliases. • Code within it causes it to read the /etc/bashrc if it exists. • /etc/bashrcis read last • It contains the global aliases. Linux+ Guide to Linux Certification, 2e

  32. Note During login different shells use different Local Environment Files Linux+ Guide to Linux Certification, 2e

  33. Summary • echo $? && || • set env export • $ PATH $HOME $SHELL $PS1 • alias set shopt • source . ./script1 bash script1 • /etc/profile /etc/bashrc • ~/.bash_profile ~/.bashrc ~/.bash_logout Linux+ Guide to Linux Certification, 2e

More Related