Bash cheatsheet

Tue 30 March 2021
# To witch shell type, e.g. zsh
chsh -s /bin/zsh
chsh -s /bin/bash
# To hide bash deprecation warning on mac add the following to .bash_profile
export BASH_SILENCE_DEPRECATION_WARNING=1

# filter output and just print the n'th line of it
bash someScript.sh | sed -n '<n> p'
# e.g. to find the first file occurence and use it as input for vim
vim \$(find . -type f -iname <filename> | sed -n '1 p')

# tired of putting yes?
sudo apt-get install -y <..>
yes | tr -d \\r\\n | <...>

# count all the lines of code in a directory recursively? (e.g. javascript)
find . -name '*.js' | xargs wc -l

# get process start-time
ps -p <PID> -o lstart=

# write stdout + stderr to file by redirecting stderr to stdout (&1) and
# then redirect stdout to a file
<command> > 2>&1
# or simply redirect both to a file
<command> &> out

# add new user
sudo useradd <username>

# add user to sudo group
sudo visudo
# and add the following:
# (below the section "Allow root to run any commands anywhere")
<username> ALL=(ALL) ALL

# grep, but only certain file extensions
grep -R --include \*.h <word> .

# get ip address on mac
ipconfig getifaddr en0

# wifi down?
sudo systemctl restart network-manager

Category: Snippets [Bash]