This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
““Small” differences among shellsSmall” differences among shells Bash has 3000+ line man pageBash has 3000+ line man page
VariablesVariables
variable format:variable format:variable = valuevariable = value(there should not be any spaces around the =)(there should not be any spaces around the =)
Example:Example:$number=7$number=7
$ echo The value of number is $number:$ echo The value of number is $number:
The value of number is 7The value of number is 7
$$
Using QuotesUsing Quotes Four types of quote charactersFour types of quote characters
‘‘ single quote charactersingle quote character
““ double quote characterdouble quote character
\\ backslash characterbackslash character
`̀ back quote characterback quote character
Example (single quote):Example (single quote):$ cat city$ cat city
AustinAustin
DallasDallas
Ft. WorthFt. Worth
San AntonioSan Antonio
$ grep ‘San Antonio’ city$ grep ‘San Antonio’ city
San AntonioSan Antonio
$$
Using QuotesUsing Quotes
Example (double quote):Example (double quote):
$ex1= “‘Dallas,’ A city in Texas”$ex1= “‘Dallas,’ A city in Texas”
$echo $ex1$echo $ex1
‘‘Dallas,’ A city in TexasDallas,’ A city in Texas
$ex2=‘ “San Antonio,” is also a city in Texas’$ex2=‘ “San Antonio,” is also a city in Texas’
$echo $ex2$echo $ex2
““San Antonio,” is also a city in TexasSan Antonio,” is also a city in Texas
$$
Using Quotes - (cont)Using Quotes - (cont) Example (backslash):Example (backslash):$echo $ex2$echo $ex2
““San Antonio,” is also a city in TexasSan Antonio,” is also a city in Texas
$$
$ echo \$ex2$ echo \$ex2
$ex2$ex2
$$
which is the same as:which is the same as:
$echo ‘$’ ex2$echo ‘$’ ex2
$ex2$ex2
$$
Using QuotesUsing Quotes
Example (back quote)Example (back quote)
$ echo The date and time is: `date`$ echo The date and time is: `date`
The date and time is: Tue Oct 10 10:18:34 EDT 2006The date and time is: Tue Oct 10 10:18:34 EDT 2006
$$
Making Decisions - IfMaking Decisions - If if format:if format:
if expressionif expression
then commandsthen commands
elif commandselif commands
else commandselse commands
fifi
Making Decisions - Test Making Decisions - Test (cont)(cont)
Making Decisions - Test Making Decisions - Test (cont)(cont)
Test file operators returns true if:Test file operators returns true if:-d -d filefile file is a directoryfile is a directory-f -f filefile file is an ordinary file, and existsfile is an ordinary file, and exists-r -r filefile file is readable by the processfile is readable by the process-s -s filefile file isn’t emptyfile isn’t empty-w -w file file is writable by the processfile file is writable by the process-x -x filefile file is executablefile is executable-G -G filefile file is owned by the group I belong tofile is owned by the group I belong to-O -O filefile file is owned by userfile is owned by user-u -u filefile set_user_id bit is setset_user_id bit is set-g -g filefile set_group_id bit is setset_group_id bit is set
Allows a value of a variable to be passed Allows a value of a variable to be passed to other subshellsto other subshells
Changing an exported variable in a Changing an exported variable in a subshell does not change the value in subshell does not change the value in the parent shellthe parent shell
var3 is a local variable so its value in test3 is 2345. In test4 it is var3 is a local variable so its value in test3 is 2345. In test4 it is null.null.
Exported VariablesExported Variables(cont)(cont)
Another Example:Another Example:$ var3=2345$ var3=2345
$ export var3 $ export var3
$cat test3$cat test3
echo var3 = $var3echo var3 = $var3
$test3$test3
var3 = 2345var3 = 2345
$test4$test4
var3 = 2345var3 = 2345
$ $ var3 is an exported variable so its value in test3 is 2345. var3 is an exported variable so its value in test3 is 2345. In test4 it is also 2345In test4 it is also 2345
Exported Variables - (cont)Exported Variables - (cont) Once an exported variable always an Once an exported variable always an
exported variable. (Unless you use unset exported variable. (Unless you use unset to destroy it.)to destroy it.)
Each subshell makes its own copy of the Each subshell makes its own copy of the variablevariable
Export with no arguments lists the Export with no arguments lists the variables that are exported to users shellvariables that are exported to users shell
env Commandenv Command
env [option] [var = val] [command]env [option] [var = val] [command] displays current environment or displays current environment or
modifies variables specified.modifies variables specified. Commands specified are executed in Commands specified are executed in
the new environmentthe new environment
SubshellsSubshells
A new shell to execute a programA new shell to execute a program Has its own environment local varsHas its own environment local vars Can’t change variable in parent shellCan’t change variable in parent shell Subshell variables destroyed on exitSubshell variables destroyed on exit
PATHPATH
Directories search for programDirectories search for program Setup during logon procedureSetup during logon procedure Directories are separated by colon Directories are separated by colon
(:)(:) Message Message “not found” “not found” is returned is returned
upon unsuccessful searchupon unsuccessful search
.profile File.profile File provided by “Systems Administrator”provided by “Systems Administrator” Executed from the users home Executed from the users home
directorydirectory Can use to customize user Can use to customize user