Intro to Bash Scripting
We want to be lazy - good to reuse things when possible
Script - used to automate execution of tasks
Bash script - script written with features available in Bash command line shell
Bash Shell Script - plain text file that contains a sequence of bash shell commands
#!/bin/bash
# File: basic.sh
# Scrip thhat prints date, current user, current directory
date
whoami
pwd
shebang
) - determines where shell is located, can specify an alternative location/alternate path./[basic.sh](<http://basic.sh>)
- this only works if basic.sh
has permission to run script, so you can do chmod a+x basic.sh
to give execute permission for current userbash <filename>
- filename is name of scriptShell Variables
x=1 # string value 1
fullname="James Huang" # note - no whitespace between variable name, equals symbols, and valued assigned, if variable value has whitespace, use quotes
word=hard
echo ${word} # hard
echo ${word}er # harder - word is substituted in
dir=~/cs136 # create variable named dir, assign to string which is path to file called cs136 - no output
echo $dir # echos directory variable
cd ${dir} # enter directory variable
ls ${dir} # list files in directory
echo ${dir}/a1 # concat characters
printenv
- list currently defined environment variables on system$PATH
- concatenation of directories separated by colons - used by unix command line shell to search for programs, with the shell sequentially searching directories in $PATH
variable - first matching program is executed, command not found otherwiseVariable | Meaning |
---|---|
${PWD} | present working directory (equivalent to executing pwd) |
${HOME} | your home directory (equivalent to ~) |
${SHELL} | your default shell |
${PRINTER} | your default printer |
${HOSTNAME} | your machine's name |
${PATH} | your default search path for commands and programs |
Command Line Arguments