DEV Community

killDevils
killDevils

Posted on

3

Bash | my personal cheat list

As a Concept Director, I don't have the chance to just dive into a single language. Switching between different languages causes forgetting basic tricks. Time is consumed by searching over codes in old repositories.

The basic tricks are not worthy to write into individual posts, so list here would be a better way to record and look up.

Sure, the list is growing as long as I am coding in Bash.

Switch to root

sudo su change to root, but not environment variables.
sudo su - change to root as well as the environment variables.
sudo -i used not often

Curl

curl -s mute curl
curl -O Write output to a local file in current working directory, named like the remote file we get.

Create new file

touch /tmp/payTime an empty file called payTime created under /tmp.

Make new directory

mkdir /tmp/business made a folder called "business" under /tmp, if /tmp exists.
mkdir /tmp/company/business made a folder called "business" under /tmp/company, even /tmp and /company do not exist.

Quick go back to the working directory of current user

cd ~ or cd

Make a .sh file executable

chmod +x or chmod a+x makes it executable for everyone.
chmod u+x makes it executable for current user.
chmod g+x makes it executable for current group.
chmod o+x makes it executable for others.

Cron

crontab -l list current user's existing cron jobs.
crontab -e edit.
crontab -r remove all cron jobs.

Variable

${var} = $var. The former is really useful when the latter meets conflict with the content behind. e.g.

folder="/usr/share/"
file="password.txt"
cat $folderfile  #this would meet a big problem.
cat ${folder}file  #this is good to use.

export var=0 set a new environment variable in the current session of the current shell. Would dismiss in the next login.

awk

awk -F@ regards @ as the divider.
awk '!\name\ && \running\ {print $1}' ignore the lines contains "name", remain the lines contains running, then grab the first column.

Date and Time

date show current date and time in standard format.
date +"%Y-%m-%d %H:%M:%S" show in "2020-05-20 15:49:28" format.
date -d "+1 hour 30 minutes" +"%Y-%m-%d %H:%M:%S" show in "2020-05-20 15:49:28" format, plus 90 minutes based on current Time.
date +%s count the seconds from 1970-1-1 0:0:0 (Epoch Time).
date -d @1344127275 +"%Y-%m-%d %H:%M" count 1344127275 seconds from the Epoch Time, show in "2012-08-05 08:41:15" format (the timestamp Curiosity rover lands on Mars 🎉).

Four Operations

echo $((1+2*3-4/5)) integer only. The result is also integer.
echo 1.2+2.3*3.4-4.5/5.6 | bc -l decimal is allowed. the result is 20 decimal digits.
printf %.3f $(echo 1.2+2.3*3.4-4.5/5.6 | bc -l) to round 20 decimal digits down to 3.

Mutt

mutt -e "set realname='Kill Devils'" show a custom name in mail instead of the string before @ of your email address.

Compare numbers

Compare decimals:

if (( $(echo "50.01 >= 50.00" | bc -l) )); then
echo "50.01 is greater equal than 50.00"
fi

Compare integers:

if [[ 51 -ge 50 ]]; then
echo "51 is greater equal than 50"
fi

Background job

bash update.sh & job would run in background, but would terminate when the current session is exit.
nohup bash update.sh & nohup = no hang up. job would not be affected by current session logging out.

Redirection (stdin, stdout, stderr, screen. std = standard)

bash update.sh > output.out stdout redirects from screen to "output.out" file.
bash update.sh 2> output.err stderr redirects from screen to "output.err" file.
bash update.sh &> output.log both stdout and stderr redirect from screen to "output.log" file.
bash update.sh | tee output.log show everything on screen, redirect everything into "output.err" file as well.

Move files

mv -f force move, ignore conflicts.

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay