Bash Script Template

#!/usr/bin/env bash

[your command here]

User and Groups

# change owner of a file or dir
chown [owner] [file]

# change pprivileges
chmod [-r] [+/-rwn] [dir]

# create a new user
adduser [user]

# list all user
cat /etc/passwd

# delete user
deluser [user]

# print groups
groups

# create a new group
groupadd

# change group privileges
groupmod

# delete group
delgroup

# change password
passwd

Logical Operator

# and, execture all otherwise none
[command1] && [command2]

# or, execute any if possible
[command1] || [command2]

# condition
if [ condition ]; then [command1]; else [command2]; fi

Pip & redirect

# make a pip 
[command1] | [command2] | [command3]

# sort files based on the name, and display it using less
ls | grep *.sh | less

# redirect stdout to file (overwrite), reverse "<"
[command] > [file]

# same as above
[command] 1>[file]


# redirect stderr to file
[command] 2>[file]

# redicect stderr and stdout to file
[command] &>name

# redirect stdout to file (append), reverse "<<"
[command] >> [file]

# discard output
[command] > /dev/null

# discard error message, same as 2>&0
[command] 2>/dev/null

# discard output and error message
[command] > /dev/null 2>/dev/null

# same as above, different from &>, &1 indicate stdout
[command] > dev/null 2>&1

Arguments

# take arg from input when execute bash scripts, e.g., example.sh --arg1
[command] "${arg1}"

# same as above
[command] $1