Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and.

Similar presentations


Presentation on theme: "The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and."— Presentation transcript:

1

2 The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and power over the defaults when issuing commands by GUI. – 2. Speed – many programs issued at the command line are text- based and therefore will start faster and run faster than GUI- based programs. – 3. Resources – as with speed, a text-based program uses fewer system resources. – 4. Remote access – if you are using a program like ssh to reach a computer, you may be limited to a text–based interface, so learning it is important and once learned, you gain the other benefits. – 5. Reduced health risk – yes, this one sounds odd, but mouse usage is one of the most harmful aspects of using a computer as it strains numerous muscles in your hand and wrist, while typing is less of a strain (although poor hand placement can also damage your wrist).

3 The Command Line Prompt You can tailor your prompt but by default, it will look like this: – [foxr@localhost ~]$ What do these parts mean? – Username – Hostname – Current directory ~ means “user’s home directory” – $ the user prompt (# for root)

4 Entering Commands Type the name of your command and press Some commands do not require additional information, so the command is just the name of the command itself (which is also a program’s name) – The following instructions output information similar to what we find from our prompt whoami – output the user’s username pwd – output the current working directory hostname – output the name of the host (this will either be localhost.localdomain or the IP address or IP alias of the machine logged in to) ls – list the contents of the current working directory

5 Other Commands passwd – used to change your password – you are first prompted to input your current password and then your new password (twice). If the password you enter is not sufficiently strong, you are warned uname – output basic information about your operating system arch – output basic information about your computer’s hardware who – list the users currently logged in to the computer you are operating on, including their terminal – which terminal window they have opened or whether they are on console) bash – start a new Bash shell (if in a shell, this starts a new session so that the outer shell session is hidden) – sh starts a new shell in the default shell type which may not be Bash exit – leave the current Bash shell and if this is the outermost Bash shell, close the window

6 Example Session [foxr@localhost ~]$ uname Linux [foxr@localhost ~]$ arch x86_64 [foxr@localhost ~]$ who foxr tty7 2013-10-11 09:42 (:0) foxr pts/0 2013-10-11 15:14 (:0) [foxr@localhost ~]$ uname; arch; who Linux x86_64 foxr tty7 2013-10-11 09:42 (:0) foxr pts/0 2013-10-11 15:14 (:0) prompt user input program output

7 Commands with Options/Parameters Most Linux commands allow or require additional information – Options – user-specified variations in how the command should be performed – Parameters – files/directories that the command should operate on The typical format for a command is – command [option(s)] [parameter(s)] The [ ] indicate “optional”

8 The ls Command The ls (list) command lists the contents of a directory or information about directories and files – examples ls – list all contents ls –l – perform a long listing (-l is lower case L) ls –l file1.txt – perform long listing on file1.txt ls –l file1.txt file2.txt file3.txt – perform long listing on several files ls –l *.txt – perform long listing on all items in this directory that end with.txt ls –a – show all files including “hidden” files ls –al (also ls –la and ls –a –l) – both ls –a and ls –l combined

9 The ls Command: Long Listings A long listing will display for each item – the type of object (file, directory, symbolic link, etc where a hyphen means a regular file) – the permissions of the object (who can read, write and execute it, which appear using r, w, and x, with a hyphen indicating no permission, we examine permissions in detail in chapter 3) – the number of names linked to the object – the user owner and group owner of the object – the size of the object – the last modification (or creation) date and time of the object – the name of the object (and if a symbolic link, the item it is linked to)

10 The ls Command: Long Listings Example long listing of a part of a directory, all of these are files with the same permissions, number of hard links, owner, group, creation date

11 The ls Command: Other Options OptionMeaning -ASame as –a exact that. and.. are not shown -BIgnore items whose names end with ~ (~ is used for backup files) -CList entries in columns (fits more items on the screen) -dList directories by name, do not list their content -FAppend listings with item classifications (ends directory names with /, ends executable files with *) -gSame as –l except that owner is not shown -GSame as –l except that group owner is not shown -hWhen used with –l, modifies file sizes to be “human readable” -iInclude inode number of items (inodes are discussed in chapter 10) -L Dereference links – that is, display information about item being linked to, not the link itself (links are discussed in chapters 3 and 10) -rList items in reverse alphabetical order -RRecursive listing (list contents of all subdirectories) -sWhen used with –l, outputs sizes in blocks rather than bytes -SSort files by size rather than name -tSort files by modification time rather than name -XSort files by extension name rather than name (numeric 1) List files one per line (do not use columns)

12 man Pages man – command to list a command’s man page (manual) – gives information about the command

13 man Pages: Types of Information EntryMeaning NameCommand name with one line description SynopsisOne or more entries that show the syntax of the command’s usage DescriptionDetailed description of what the command does Options Description of every available option, including additional parameters expected by options, and options that options can have (options may be listed under description) Arguments/ParametersAdditional parameters expected or allowed EnvironmentEnvironment variables that this command might use BugsKnown errors or fixes NotesAdditional information that might be useful FilesFiles that this command might access Exit or return valuesError or exit codes ExamplesIn rare cases, some man pages include examples of how to use the command FunctionsNames of functions that can be called by the command Author(s)Person/people who wrote the command’s programming code See alsoOther Linux instructions related to this one or worth exploring

14 man Pages: Moving Through Them forward one screen – ‘f’, ‘z’, space bar forward half a screen – ‘d’ forward one line – ‘e’, ‘j’, enter key, down arrow back one screen – ‘b’, ‘w’ back one half screen – ‘u’ back one line – ‘b’, up arrow return to top of man page – ‘g’ go to bottom of man page – ‘G’ go to line # - #G (# is a number like 10G for line 10) obtain help – ‘h’, ‘H’ to move forward # lines - # (# is a number) to search forward for a string - /string to search backward for a string - ?string exit man page – ‘q’, ‘Q’

15 The apropos Command Used to list all commands whose description matches a given string – the description is taken from all of the man pages – format: apropos string or apropos “string” if you use “” then the string must match exactly whereas if you don’t and the string is multiple words, then apropos matches descriptions that contain any of the words – example: apropos virtual memory lists over 100 instructions while apropos “virtual memory” lists mremap (2)- re-map a virtual memory address vfork(3p) - create a new process; share virtual memory vmstat (8) - Report virtual memory statistics

16 Bash Features: History Every instruction entered is stored in a history list You can recall instructions from the history list to save time from re-typing a previous instruction To obtain your history, use the history command – history – lists the previous n instructions where n is a default value – history n – list the last n instructions in the history list

17 Bash Features: History Given the history list on the right, how do we recall instructions? – control+p or up arrow to backward through the history list – control+n or down arrow to move forward through the history list – !! – re-execute last instructino – !# - re-execute instruction # (e.g., !10 to repeat cat script1) – !str – re-execute most recent instruction that starts with the string str ( !c re- executes 10 while !cd re-executes 8) 1ls 2cd ~ 3pwd 4man cd 5ls –l 6cd /bin 7ls –l | grep root 8 cd ~ 9vi script1 10cat script1 11rm script1 12history

18 Bash Features: History Other history options aside from n -c – clear the history list -d # – where # is a number, delete that numbered entry from the history list -w – write the current history list to the history file -r – input the history file and use it instead of the current history # – where # is a number, display only the last # items in the list

19 Bash Features: Variables A variable is a name given to a storage location – In Bash, variables by default store strings but can also store integer numbers (no decimal point) or arrays of strings/integers Three types of variables – Normal variables are defined from the command line and available in your current session – Script variables which are known only in the script – Environment variables are defined in a script but exported to be known in your environment

20 Bash Features: Variables You assign variables their values in an assignment statement – var=value var is the name of the variable which can consist of letters, digits and underscores (_) as long as the name does not start with a digit value is a literal value, the output returned from a Linux program, the value stored in another variable, or the result of some operation on the value stored in a variable or any combination of these values which contain blank spaces must be entirely enclosed within quote marks (“” or ‘’ although we prefer “”)

21 Bash Features: Environment Variables These variables are pre-defined at the time the user logs in and opens a shell VariableMeaning (value) DESKTOP_SESSIONGUI being used (Gnome, KDE) DISPLAYThe terminal window being used, denoted as :0.0 if you are logged in through a GUI HISTSIZENumber of entries retained in the history list HOMEUser’s home directory name HOSTNAMEName of computer LOGNAMEUser’s user name MAILLocation of user’s email directory OLDPWDLast directory visited before current directory (see PWD) PATHThe list of directories that the interpreter checks with each command PS1Specifies the user’s prompt (explained in more detail in table 2.5) PWDThe current directory SHELLName of the program being used as the user’s shell (for Bash, this is usually /bin/bash) TERMThe terminal type being used USERThe user’s username USERNAMEThe user’s name (if provided)

22 Bash Features: PATH Variable Whenever you type in a command, the interpreter must locate the executable program of that command – Many are stored in /bin and /usr/bin To prevent the user from having to type in the full path, such as /bin/ls or /usr/bin/apropos, we use a variable called PATH – This variable stores directories that the interpreter will test in searching for the program

23 Bash Features: PS1 Variable PS1 encodes the information that is displayed in your prompt – for instance: [\u@\h \d] $ is your current PS1 CharacterMeaning \uUser name \hHost name \wCurrent working directory \!Number of current command (as it will appear in the history list) \tCurrent time \dDate (as in Mon Aug 13) \$?Status of last command \aBell character (makes a tone) \jNumber of jobs in the current shell (we explore jobs in chapter 4) \nNew line [`command`]Output of the Linux command

24 Bash Features: echo The echo command is an output statement to output the value stored in a variable – Format: echo string – If the string is to include variable names, put a $ before each variable name as in $var to output the value stored in the variable – If name=Richard then echo $name outputs Richard while echo name outputs name

25 Bash Features: echo echo has a few options of note – -n – do not output a new line character at the end of the output so that the next echo statement continues on the same line – -e – enable \ escapes (see table below) – -E – disable \ escapes Escape CharacterMeaning \\Output a \ \b Backspace (back cursor up 1 position) \nNewline – start a new line \tTab \! \$ \& \; \' \"!, $, &, ;, ' and " respectively

26 Bash Features: alias In Linux, an alias is a name applied to a command You define aliases – to reduce the amount of typing – so that complicated instructions do not have to be remembered – so that common typos made by the user can be interpreted correctly – so that dangerous commands are made safer Format: alias name=command – If the command includes spaces, enclose the entire command in ‘’

27 Bash Features: alias Examples – alias..='cd..'less typing – alias lss=less common typo – alias sl=ls common typo – alias mountcd='mount /dev/cdrom /cd iso9660 ro,user,noauto' long or hard to remember – alias rm='rm –i' safety To remove an alias, type unalias name – as in unalias sl To see all defined aliases, type alias by itself

28 Bash Feature: Command Line Editing Bash has a number of useful keystrokes that save you from excessive typing on the CLI These are combinations of control or escape characters – control+key means hold control and press key – escape+key means press escape and then press key These sequences are based on the emacs editor (see chapter 5) Learning these can save you a lot of time and effort!

29 Bash Features: Command Line Editing KeystrokeMeaning ctrl+aMove cursor to beginning of line ctrl+eMove cursor to end of line ctrl+n (also up arrow)Move to next instruction in history list ctrl+p (also down arrow)Move to previous instruction in history list ctrl+f (also right arrow)Move cursor one character to the right ctrl+b (also left arrow)Move cursor one character to the left ctrl+d (also delete key)Delete character at cursor ctrl+kDelete all characters from cursor to end of line ctrl+uDelete everything from the command line ctrl+wDelete all characters from front of word to cursor ctrl+y Yank, return all deleted characters (aside from c+d) to cursor position ctrl+_Undo last keystroke esc+fMove cursor to space after current word esc+bMove cursor to beginning of current word esc+dDelete the remainder of the word

30 Bash Features: Redirection By default, Linux programs obtain input from STDIN (standard input) and send output to STDOUT (standard output) You can alter this through redirection – > - redirect output from terminal window to file (if file already exists, it is deleted and a new one created) – >> - redirect output from terminal window and append to file – < - redirect input from keyboard to file (nearly all Linux instructions receive input from file so this is not usually necessary) – << - redirect input from file to keyboard – | - pipe, or redirect output to be input to another Linux instruction

31 Bash Features: Redirection For instance, we want to combine several files using the cat (concatenate) command – cat file1.txt file2.txt file3.txt > joined_files.txt Without the redirection, the combined files are displayed to the screen

32 Bash Features: Redirection Here we redirect the output of the ls command of several users’ home directories to the file user_entries.txt – At first, we create the file anew and then we append the remainder of the commands to the same file ls /home/dukeg > user_entries.txt ls /home/marst >> user_entries.txt ls /home/underwoodi >> user_entries.txt ls /home/underwoodr >> user_entries.txt ls /home/zappaf >> user_entries.txt

33 Bash Features: Redirection For input from keyboard, we use << as in – cat << now whatever we type from keyboard is echoed to the terminal window but this includes so how do we end input? Use control+d We can also use cat << string – End input once you type string We might combine to create a file of text we enter by the command line – cat file.txt

34 Bash Features: Redirection The pipe is very useful to take output from one program and feed it as the input to another – ls | less Display the contents of the current directory but use the less program to display it – less displays 1 screen at a time and lets you scroll forward and backward cat somefile | less – Again, display the content 1 screen at a time cat file.txt – like the previous example to create a file from keyboard input but this time the input is sorted before being stored to file

35 Bash Features: Wildcards The wildcard is a character used to match against filenames (and directory names) The simplest wildcard to understand is * which means “match anything” – ls *.txt means list all files whose name ends in.txt – ls * matches all items in the current directory – ls a* matches all items that start with an a To other useful wildcards are ? (match any one character) and [ ] (match any one character in the brackets) – We will explore these in more detail in chapter 3

36 Bash Features: Tab Completion The Bash interpreter can complete directory and file names for you – Type a portion of the name that is unique, press, Bash completes it – If the portion is not unique, Bash emits a beep – Press and Bash displays all directories/files that match if there are too many, you are told this in a message like Display all 153 possibilities? (y or n) – Tab completion is very handy (if you remember to use it!)

37 Special Files There are several files that define initial aliases, functions and variables – these allow you to tailor your shell as you like These are script files, automatically executed when you first log in (/etc/profile, ~/.profile) or open a (new) Bash shell (/etc/bashrc, ~/.bashrc, ~/.bash_profile) – Some are located in /etc and can be modified by the system administrator – Some are located in the user’s home directory for the user to modify If you modify a file, to have it take effect, use source as in source.bashrc

38 Other Shells: csh The C shell uses syntax similar to the C programming language – Introduced numerous features now found in Bash History Command line editing Aliases Directory stack (see chapter 3) ~ Escape completion (instead of tab completion) Job control (see chapter 4)

39 Other Shells: TC Shell Updated version of C shell, T for TENEX operating system, or tcsh Updated history commands KeystrokeMeaning !!Execute previous command !nExecute command n on the history list !stringExecute the most recent command that starts with the characters string !?stringExecute the most recent command that contains the characters string command !*Execute command but use all arguments from the previous command command !$Execute command but use the last argument from the previous command command !^Execute command but use the first argument from the previous command command !:nExecute command but use the nth argument from the previous command command !:m-nExecute command but use arguments m-n from the previous command

40 Other Shells KornShell (ksh) released in 1983 – Includes WYSIWYG command line editing Almquist shell (ash) released in 1989 – Scaled down and faster version of Bourne shell (sh) Z Shell (zsh) released in 1990 adds spell checking to features found in sh and ksh

41 Comparing Bash and TC Shell FeatureBashTC Shell User prompt$% Assignment statement format var=valueset var=value Export statementexport var=valuesetenv var value Alias definition formatalias term=commandalias term command Default user definition file.bashrc.cshrc Automated spelling correction noyes

42 The Bash Interpreter Upon entering a command, the Bash interpreter takes over performing the following steps in this order – The input is broken into individual tokens a token is a known symbol (e.g., <<, |, ~, *, !, etc) or a word separated by spaces where the word is a command or a word separated by spaces where the word is a filename, directory name or path or an option combining a hyphen (usually) with one or more characters (e.g., -l, -al). – If the instruction has any quotes, those quotes are handled. – If an alias is found, that alias is replaced by the right- hand side of the definition.

43 The Bash Interpreter – The various words and operators and now broken up into individual commands (if there are multiple instructions separated by semicolons). – Brace expansion unfolds into individually listed items. – ~ is replaced by the appropriate home directory. – Variables are replaced by their values. – If any of the commands appears either in ` ` or $( ), execute the command (we explore this in chapter 7). – Arithmetic operations (if any) are executed. – Redirections (including pipes) are performed. – Wildcards are replaced by a list of matching file/directory names. – The command is executed and, upon completion, the exit status of the command (if necessary) is displayed to the terminal window (if no output redirection was called for).


Download ppt "The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and."

Similar presentations


Ads by Google