105 Shells and Shell Scripting KT Flashcards
105.1 Customize and use the shell environment
The following is a partial list of the used files, terms and utilities:
. source /etc/bash.bashrc /etc/profile env export set unset ~/.bash_profile ~/.bash_login ~/.profile ~/.bashrc ~/.bash_logout function alias
.
Command: Read and execute commands from the filename argument in the current shell context.
. filename [arguments]
source
Command: Read and execute commands from the filename argument in the current shell context. source is a synonym for dot/period ‘.’ in bash, but not in POSIX sh, so for maximum compatibility use the period.
source filename [arguments]
/etc/bash.bashrc
System‐wide .bashrc file for interactive non-login shells.
The Debian version of bash is compiled with a special option (-DSYS_BASHRC) that makes bash read /etc/bash.bashrc before ~/.bashrc for interactive non-login shells. So, on Debian systems, /etc/bash.bashrc is to ~/.bashrc as /etc/profile is to ~/.bash_profile.
/etc/profile
/etc/profile: system‐wide .profile file for the Bourne shell.
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
env
Command: Set each NAME to VALUE in the environment and run COMMAND. Run a program in a modified environment.
env [OPTION]… [-] [NAME=VALUE]… [COMMAND [ARG]…]
export
Command: Set export attribute for shell variables. Marks each NAME for automatic export to the environment of subsequently executed commands. If VALUE is supplied, assign VALUE before exporting.
export [-fn] [name[=value] …]
export -p
set
Command: Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables.
set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …]
unset
Command: Unset values and attributes of shell variables and functions. For each NAME, remove the corresponding variable or function.
unset [-f] [-v] [-n] [name …]
~/.bash_profile
The ~/.bash_profile file is a configuration file for configuring user environments. The users can modify the default settings and add any extra configurations in it.
~/.bash_login
The ~/.bash_login file contains specific settings that are executed when a user logs in to the system.
~/.profile
The ~/.profile file is yet another configuration file that is read in the absence of the ~/.bash_profile and ~/.bash_login files.
~/.bashrc
User specific .bashrc file. ~/.bashrc: read and executed by bash(1) for interactive non‐login shells.
~/.bash_logout
The ~/.bash_logout file contains instructions for the logout procedure.
function
Command: This builtin command is used to create a new function. A function is a list of commands that will be executed when the name of the function is entered.
function [OPTIONS] NAME; BODY; end
alias
Command: An alias definition provides a string value that shall replace a command name when it is encountered.
alias [alias-name[=string]…]
105.2 Customize or write simple scripts
The following is a partial list of the used files, terms and utilities:
for while test if read seq exec || &&
for
A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.
for VARIABLE in 1 2 3 4 5 .. N do command1 command2 commandN done
while
he bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition.
while [ condition ] do command1 command2 command3 done
test
Exit with the status determined by EXPRESSION. Check file types and compare values. test is the same as [.
test EXPRESSION test [ EXPRESSION ] [ ] [ OPTION
if
if statements enable different courses of action to be taken in a shell script, depending on the success or failure of a condition.
if [ ]
then
fi
read
Reads a single line from the standard input, or from file descriptor FD if the -u option is supplied.
read [-ers] [-a array] [-d delim] [-i text] [-n nchars]
[-N nchars] [-p prompt] [-t timeout] [-u fd] [name …]
seq
Print numbers from FIRST to LAST, in steps of INCREMENT.
seq [OPTION]… LAST
seq [OPTION]… FIRST LAST
seq [OPTION]… FIRST INCREMENT LAST
exec
Execute COMMAND, replacing this shell with the specified program.
exec [-cl] [-a name] [command [arguments …]]
[redirection …]
||
|| will evaluate the right side only if the left side exit status is non-zero (i.e. false).
[ Eval 1 || Eval 2 ]
&&
The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true)
[ Eval 1 && Eval 2 ]