Note
- Bash is the GNU Project's shell—the Bourne Again SHell.
directory=reports
filenames="notes.txt picture.jpg movie.mov"
echo $directory
${variable}_report
Note Shell variables have no data type. They simply store strings. Naming: use letters, numbers, underscore. First letter: a letter or underscore. Case-sensitive. Predefined variables are uppercase. PATH, HOME, SECONDS, IFS, etc.
create_report.sh A5 myfolder
Use -- (end of options) - If your input is unpredictable you can run into problems when your variable contains a string which is starting with a -
grep -- "user_input"
Note Some commands don't support this - use printf instead of echo
printf "I will delete this file: %s\n" "$file_to_delete"
Note 0-255, 0 means success Other values are error codes
Good habit Exit scripts with a correct value Always call exit with a value
if touch /tmp/x; then ...
if [[ $str ]]; then
...
fi
[[ ! $1 ]]
[[ $str ]]
[[ $str = "something" ]]
[[ -e $filename ]]
[[ ! -e $file ]]
[[ -d $dirname ]]
[[ -d $dir && $1 = "foo" ]]
[[ $a || $b ]]
man test
#!/bin/bash -x
./myscript.sh
myscript.sh
chmod u+x filename
chmod a+x filename
chmod a-x filename