Bash Script
1. Shortcut command
1.1. Simple alias
alias agi='sudo apt-get install'
Add line to end of ~/.bashrc file.
1.2. Start a script
#!/bin/bash
echo "changing directory.."
cd ~/project/wayneyee.github.io
echo "starting jekyll.."
bundle exec jekyll serve
Add to $home/bin.
Make file executable sudo chmod +x filename.
Add line to end of ~/.bashrc file.
export PATH=$PATH:$HOME/bin
exportmake variable available for children of this terminalPATHwork on variable named PATH=assign it a new value, which is$PATHthe old value:a separator$HOME/binand your newly created bin directory
Start using the new bashrc file:
source ~/.bashrc
1.3. Examples
Add to $home/bin.
2. Write to File
# overwrite
output > filename
# append
output >> filename
echo "this is a line" > file.txt
echo "this is a line" | tee file.txt
echo "this is a line" | tee -a file.txt
cat << EOF > file.txt
The current working directory is: $PWD
EOF
3. Bash Script Basics
Script starts with a “shebang” for the shell to decide which interpreter to run the script for example bash, tsch, zsh, Perl, Python etc.
#!/bin/bash
#!/usr/bin/env python3 #!/path/to/interpreter
To set the script executable run chmod +x scriptname.
+add permissionxexecutable
3.1. Variables
#!/bin/bash
SOURCEDIR=/home/user/Documents/
echo "path" $SOURCEDIR
3.2. Taking input
#!/bin/bash
echo $1
#!/bin/bash
echo -e "Please enter your name: "
read name
echo "Nice to meet you $name"