Professional Documents
Culture Documents
Lecture03 Environment Commands
Lecture03 Environment Commands
Lecture03 Environment Commands
• Line 2 creates a variable called STR and assigns the string "Hello World!" to it.
Then the value of this variable is retrieved by putting ‘$’ sign at the beginning.
Semester 2, 2019 COMP7044: Systems Scripting 3
Variables: Warning
• Bash scripting does not check variable types. This means that a
variable can hold either number or character data.
• Example:
count=0
count=“Money”
• Changing the variable value TYPE can lead to confusion for the
programmer or someone trying to modify a script, so it is
recommended to use a variable for only a single TYPE of data value in
a script.
• \ is the bash escape character and it preserves the literal value of the
next character that follows it.
Semester 2, 2019 COMP7044: Systems Scripting 4
Environment Variables
Name Meaning
$HOME Absolute path to your home directory
$PATH A list of directories to search for commands
$SHELL Absolute path to login shell
$LOGNAME/$USER Your user/login name
$PS1 Primary prompt
$PS2 Secondary prompt
$MAIL Absolute path to mailbox
$TERM Type of your terminal
• If we type in
mkdir ~/bin
and we include the following lines in the ~/.bash_profile or ~/.bashrc:
PATH=$PATH:$HOME/bin
export PATH
• Example
• sparameter.sh
• Calling it as:
./sparameters.sh arg1 arg2
• Output:
2; ./sparameters.sh; arg1; arg2; arg1 arg2; arg1 arg2
• Examples:
• ls > log.txt # Redirects the listing output into log.txt
• wc < log.txt # Counts the number of words, characters or lines from log.txt
• echo “Hello” >> content.txt # Appends “Hello” to the file content.txt
• Regular expressions are used when you want to search for specific
lines of text containing a particular pattern.