08. The Shell (Part 2)

Topics:
  • Job control
    • Foreground and Background Jobs
    • Running a Job in Background
  • Alias
  • Variables and environment variables
  • Saving environment customization
  • Command history
  • Different shells


Foreground and Background Jobs
  • When the shell starts a process to run a utility or a shell script for you, the process is also called a job
  • The default way for the shell to run a job is to run it in the foreground, which means the shell doesn’t accept any other command from you while it’s waiting for the job to finish running
  • If the job that the shell is running in the foreground takes a long time to finish, it means you wait a long time before you can enter another command. When this happens, you want to run the job in the background instead.
  • When the shell runs a job in the background, it starts the process to run the job, and then immediately prints the prompt to wait for your next command. When the background job finishes, the shell will print a message on screen to let you know that the job is finished.

Running a Job in the Background
  • To run a job in the background:    command &
    • command can contain options and arguments and redirection
    • if command contains options and arguments, the & must be at the end of the command line for the options and arguments to be in effect
  • Examples of multiple commands on the same command line:
    • command1;  command2 &
      • command1 runs in foreground, command1 finishes, then command2 runs in the background
    • (command1; command2) &
      • command1 runs in the background, finishes, then command2 runs in the background
    • command1 &  command2 &
      • command1 and command2 both run in the background, at the same time. Note that there is no ;  to separate the 2 commands on the command line for this case
    • command1 | command2 | command3 &
      • the whole pipe (starting with command1 and ending with command3) runs in the background

Job Control
  • To see the status of a job:   jobs
  • jobs return the job number, command name (your request to start a job), and status of all jobs that are running by the shell
  • Status of a job:
    • running in foreground: currently running and the shell is not accepting your input
    • running in background: currently running and the shell is waiting for your input
    • stopped: the job is suspended and all job status are saved. When the job runs again it will continue from when it was stopped
    • terminated: the process running the job is gone
  • If you don’t have a job running in the foreground, you can change the status of a job by using shell commands
  • If you have a job running in the foreground, you can change the status of a job by using interrupt signals

To switch from one job status to another:
  • n is the job number. If there is only one job, the %n argument is not needed for the commands fg and bg. It is always needed for the command kill
  • The kill command can accept multiple job numbers
  • Switching the status does not change the input, output, or error destination

alias
  • Use alias to customize a command
  • 2 common reasons to customize a command:
    • change the command name to something you like
    • have the command always run with the options you like
  • To create a command alias:    alias   aliasName=‘existingCommand’
    • aliasName is usually short
    • existingCommand can contain options or arguments
    • The single quotes around existingCommand is required if there are spaces or metacharacters in the command
    • There is no space before or after the =
  • To use the alias, type the aliasName on the command line. The existingCommand with its options and arguments will run.
  • An alias created on the command line will exist only for the current session
  • To remove an alias:   unalias   aliasName
  • To see all aliases, both the aliases you create and the system aliases:      alias

Variables
  • Variables are small blocks of memory you can claim, in order to store your data temporarily
  • When you create a variable you give it a name, and you access the memory by using that name. The variable name follows the same rule as a filename.
  • To create a variable:    variableName=data
    • data can be a number, a character, a word, or a text string. You can use quotes if there are metacharacters in the text string.
    • There is no space before or after the = sign
  • To change the data in a variable:    variableName=newData
  • To display the data stored in a variable:   echo  $variableName
    • The $ symbol tells the shell to fetch the data at the memory location with the name variableName. The $ symbol has this special meaning for the shell when it’s not quoted or when it’s double quoted. When the $ symbol is single quoted, it means the literal $ symbol.

Environment Variables
  • The shell keeps track of settings for your account.  The settings that are specific to your account and affect your work session are called environment settings, and are saved in environment variables
  • To see all environment variables:    env
  • To see a single variable:    echo  $VAR_NAME
    • VAR_NAME is the variable name, always in uppercase
    • the $ causes the shell to fetch the data from the variable
  •  Some basic environment variables
    • HOME:  the absolute path of your home directory
    • PS1: your primary prompt
    • PS2: your secondary prompt
    • PATH: the paths of all the directories that the shell will search to find the executable that you ask it to run
    • SHELL: your default shell type
  • You can change some environment variables to customize your work session
  • PS1: the default prompt displays your userID, the system name and your working directory. You can change it to display any text you like
  • PS2: the secondary prompt displays the > symbol. You can also change it to any text you like
  • HOME and SHELL: are set by system admin and you cannot change them. They are available for you to use, not to set.
  • PATH: users often modify this variable to add more search paths. If you have executable files in your directories, appending these directory paths to PATH will make it possible for you to run the executable files by just typing their name on the command line. 
    • To append to PATH:       PATH=$PATH:new_directory_path

Saving Your Setting Preferences
  • So far we’ve learned different ways for you to customize your settings. However, because the customization is done on the command line, they only affect your current session
  • For the customization to last from session to session, you need to save them in special files
  • There are 2 hidden files in your home directory that the shell will run during initialization:  .bashrc and .bash_profile. They are both text files. Any change to these files are optional.
  • .bashrc: save your aliases and your messaging preference
    • Each alias is 1 line in the file:  alias  aliasName=‘command’
    • Have a line for messaging status:    mesg  n    (or y)
  • .bash_profile: save your environment variables and permission mask
    • Each environment variable change has a line:  VAR=data          and in the EXPORT line, add the name of the variable
    • Have a line for your default permission mask:    umask  value

Command History
  • The shell keeps track of your most recently run commands in an internal buffer
  • The size of this buffer is set by the environment variable HISTSIZE, which you can display and change. 
  • The default size is 1000, the last 1000 commands you typed in are saved
  • The buffer is circular, so after 1000 commands, the oldest (earliest) ones are overwritten by the newest commands
  • To display the saved commands in the buffer:  
    • history           displays all the commands in the buffer
    • history  n       n is a number, displays the last n commands 
  • Each command is displayed with a number that corresponds to the order that the command was run
  • Alternatively, each command in the buffer can be displayed one by one on the command line. To scroll through the buffer and display each command one by one:  use the up arrow key
  • To re-run the last command:   !!
    • To re-run any previous command, there are 2 ways:
      • Use the up arrow key to scroll through the history buffer one command at a time
        • When you get to the command you want, hit enter to run the command
      • Use the history utility to see the last n commands along with their associated numbers
        • Then use:   !num     
        • where num is the number associated with the command
    • To modify a previous command before running it:
    • Scroll through the buffer to find the command
    • Retype any change in the command
    • Hit enter to run the new command
    • This is useful when you have a long command to modify

Shell Types
  • The shell is your interface to the system, but internally it is a program that runs when you successfully log in, and terminates when you log out.
  • There are 4 popular types of shells that Unix/Linux users run
  • Bourne: named after its author, it’s the oldest shell that came with Unix
  • C Shell: came from UC Berkeley when Unix was distributed to all the universities. It is not compatible with the Bourne shell
  • Korn: named after its author, it’s built upon the Bourne shell so it is similar to the Bourne shell but has more features
  • Bash: (for Bourne Again Shell) it’s the default shell for Linux. It’s built upon the Bourne shell also
  • The environment variable to display your default shell is SHELL

Changing Shells
  • The default shell is the shell that automatically runs when you first log in
  • System admin sets the default shell for you, and you need to ask them if you want to change your default shell
  • Within a working session, you can change your shell to any type you like
    • To change to a Bourne shell: sh
    • To change to a C shell:     csh
    • To change to a Korn shell:   ksh
    • To change to a bash shell:   bash
    • To terminate a shell:   exit 
  • Each time you change shell, your current shell starts a new process to run the new shell.  
  • If you change shell 2 times from when you log in, then you have 3 processes running: your default shell and 2 other shell processes. To completely log out, you will need to type exit 3 times. Each exit gets you back to the previous shell.
  • To see which shell you are currently running:  echo  $0
  • The utilities you learn in this notes are standard Linux utilities that will work the same way, no matter which shell you are running.  This is because the utilities are not a part of the shell, the shell only interacts with the utilities when you request them. 
  • When you write shell scripts, however, it is important what shell you run. This is because shell scripts use syntax that are specific to a certain shell type.  For example, a script written for the C shell, which uses C shell syntax, will not run on bash.


Comments