Shell

up:: 05 Programming MOC

Dotfiles

Dates

Commands

  • Stop a process with Cmd + c

File navigation

pwd # Show current directory
ls # List all contents in current directory
cd # Enter directory (eg. "cd Desktop")
cd .. # Bring you 'up' one directory
 
./md-cal.sh # run script
chmod +x shell.sh # make executable

Copy to clipboard

ls | pbcopy

Misc

  • Control + c: Exit out of running program
  • To debug programs ShellCheck is amazing.
    • shellcheck yourscript.sh

File editing

cp # copy
mv # move
	# This also works with multiple folders:
	mv a.txt b.txt c.txt folder/
rm # remove
 
cat # show contents of text file
less # show contents of text file in new window
 
grep # grab specific data
 
touch file1.txt file2.txt # create file(s)

Variables

# No space between variable assignment
variable="Test" # Not: variable = "Test"
printf '%s\n' "${my_array[@]}" # Print array

Regex

grep "^\- \[ \]*" # Search for "- [ ]"
 
echo "- [ ] A task" | grep "^\- \[ \]*" # pipe through command

Calling functions

myFunc() {
	echo "All commands"
	echo "Go here"
}

Crontab

🔖 Crontab.guru - The cron schedule expression editor 🔖 How to Fix Cron Permission Issues in MacOS Catalina & Mojave

crontab -l # List crontabs
crontab -e # Edit crontabs
0 12 * * *  cd ~/my/backup/folder && ./backup.sh # Example for backup
*/30 * * * * <command-to-execute> # Run every 30mins

Transclude of Create-a-symbolic-link

Passing arguments to a script

  • $1: First variable passed
  • $#: Number of variables passed
  • $@: all variables passed

Iterate over all inputs

for var in "$@"
do
    echo "$var"
done