Sobell ch 8: The Bourne Again Shell (Bash)
quoting $
"$variableName" does not work '$name' and /$name do work
-x oprtion
#! /bin/bash -x turns on debugging
commands that are symbols
(,),[,],
order of files shell looks for
.bash_profile .bash_login .profile
what will this yield false && false || true echo $?
0 true
what will this yield (false && false) || true echo $?
0 true
what will this yield? true || false && false echo $?
1 (false)
what will this yield? (true || false) && false
1 fasle
&&
Boolean AND short-circuiting control operators makes shell test exit status of command before. if command succeeds, goes to next, else skips next ex mkdir bkup && cp -r src bkup
suspending a program
CONTROL -Z temporarily stops incudes message that says stopped
keyword variables
HOME, PATH
how to declare and initialize a variable in bash
VARIABLE=value no white space characters on either side of = ex person=max echo person *person* echo $person *max*
chmod u+x filename
add execute permission (x) for the owner (u)
job
another name for process runing a pipeline can have mamny jobs running by seppearating commands with &&
(())
arithmetic expansion
&
background task shell executes task in the background and display prompt immediately to work on other tasks will display job number in [] and PID of each process running will then display done if done can layer: d & e & f &
bash shell
based on bourne shell
creating a shell to get input from a file OR supply commands to bash using standard input
bash filename do not need exectue permission for filename. only read permission OR bash < filename
#
begins a comment (whole line)
||
boolean OR short-circuiting control operators remaining commands execute only if first command failed ex mkdir backup|| echo "mkdir of bkup failed" >> /tmp/log
fg
brings a job to the foreground shell assigns job number. use that ex fg 2
is shell variable available only in the shell it was created?
by default yes. they are local
what can a shell script do
can run a utility, program (compiled), or another shell script
;;
case terminator
chsh
chamges shell pore permanently and anter password and pathname of shell you want to use (/bin/bash )
nameOfShell RETURN
changes shell login nameOfShell RETURN (bash, tcsh, another shell) give exit command to return to previous shell
[n]<&-
closes standard input or file descriptor n if specified
[n]>&-
closes standard output or file descriptor n if specified
NEWLINE
command initiator initiates execution
;
command separator
$()
command substitution
standard error
commands sending output to this. to keep from getting mixed up with information it sends to standard output by default to the screen
[[ ]]
conditional expression
.bash_logout
contains commands to log out. clean up after a session. remove temporary files
/
continue a command to next line. when command is long
PATH
determines which directories shell searches
dirs
displays the contents of directory stack if empty, displays working directory
which is the faster way to run a shell script
do not use bash to execute. instead give yourself execute permission and directly invoke script
<&m
duplicates standard input from file descriptor m
[n]>&m
duplicates standard output or file descriptor n if specified from file desccriptor m
bash -e
errexit causes bash to exit when simple command fails helps make program less fractious
exec
execute
./
execute ex ./whoson
korn shell
expanded from Bourne Shell nd similar to BASH
shell script
file that holds commands the shell can execute
startup files
files that initialize shell when it starts
login shells
first shell that displays prompt when login on system
( )
groups commands
/etc/profile
holds commands the shell executes. allows users to work with root privileges.
$?
holds exit status of preceding command false; echo$? 1
HOME
identifies home directory
exit status
if it executed successfully or not 0 is true 1 is false
jobs
lists jobs running (ex like if you put something to sleep)
chmod
makes file executable
to execute a file,
must have permission to read and execute
do any of the control operators need to be surrounded by white spcae?
no but it makes it easier to read
does sh call the original bourne shell?
no. its symbolic link to bash or dash command bash tries to mimic behavour of original bourne shell - doesnlt always work
.bashrc
nonlogin shell that interacts
bash -u
nounuset display message and exit when nit tries to eexpand an unset variable ex #! /bin/bash -eu
Sh
original Bourne Shell on UNIX system
|
pipeline returects output of one command to be input of next command
file descriptor
place a program sends its output to and gets input from execute program, shell opens: 0(standard input) 1 (standard output) 2 (standard error)
popd
pops directory off the stack working directory is new top directory popd +1
POSIX
portable operating system interface.
pushd
pushes a directory on the stack when pushd w/o arg, swaps top two directories on stake. top directory is second directory easily move back and forth between directories (2)
>> filename
redirects and appends stanard output to filename. creates if dne
<
redirects standard input
>
redirects standard output ex: cat x y > hold *error that x dne will pop up* *ys stuff will be in hold*
&> filename
redirects standard output and standard error to filename
>! filename
redirects standard output to filename
bg
sends job to the background must first suspend ctr Z
; and NEWLINE
separate commands can use ; to write a sequence of commands on one line initiate execution pressing RETURN
() groups command
shell creates a copy of itself (subshell) for each group and creates new process to execute
~
short for home directory
#!
specifies a shell. put it in first line of shell script to tell OS which shell (program) should execute file + options ex: #!/bin/bash
|&
standard error pipeline
variables
start with letter or underscore no number
where to change keywords variables?
startup files
()
subshell
[ ]
test command
- under permissions
that user does not have permission
precedence of && and ||
they are the same unless parenthesis are used. else will be equally evaluated from left to right
special paramters
those like last command executed satus of recently executed command. you cannot assign these
user created variables
those user creates can change at any time or make them readonly
control operators
tokens that separate, temrinate, and group commands
?
true false utilities do nothing. return 0 or 1 exit statuses ex: false; echo$?
ps -f
underneeath first line in shell script displau name of program executing script
having spaces in variables
use " double quotes person="mac and zac"
what should you do if you only want to run using a different shell or if bash is not you rinteractive shell
use the bash command
dash shell
user login shell. executes more quickly than bash
shell parameter
value you or shell can access like user created variables keyword variables positional parameters special parameters
declare and initialize variable in a script
variable=/home/sam/temp
run a command with variable set to something
variable=/home/sam/temp ./my_script