IGPenguin / terminal-cheats

💾 Bash oneliners and terminal tricks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is it?

💾 Bash oneliners and terminal tricks
⚠️ Sourced by Google search, use at your own risk.
⭐️ Hit the star button, make me happy!
👀 Visit my other projects https://github.com/IGPenguin

Table of contents

Basic bash syntax

Command chaining

command1; command2; command3; - execute command regardless of success of the previous command
command1 && command2 - execute command2 if command1 executed successfully (exit code 0)
command1 || command2 - execute command2 if command1 execution failed (exit code != 0)
command1 & - execute command as a background task
wait - wait until triggered background tasks are done

Variable comparsion

if [ "$var" -eq 1 ]
Always use spaces between brackets and the condition
Always quote a tested variable

Integer comparison operators

-eq - equal to if [ "$a" -eq "$b" ]
-ne - not equal to if [ "$a" -ne "$b" ]
-gt - greater than if [ "$a" -gt "$b" ]
-ge - greater than or equal to if [ "$a" -ge "$b" ]
-lt - less than if [ "$a" -lt "$b" ]
-le - less than or equal to if [ "$a" -le "$b" ]
<, <=, >, >= - within double parentheses (("$a" < "$b"))

String comparison operators

-n - string is not null if [ -n "$string" ]
-z - string is null, that is, has zero length if [ -z "$string" ]
= or == - equal to, if [ "$a" = "$b" ]
!= - not equal to, if [ "$a" != "$b" ]
The == comparison operator behaves differently within a double-brackets test than within single brackets
[[ $a == z* ]] - true if $a starts with an "z" (pattern matching)
[[ $a == "z*" ]] or [ "$a" == "z*" ] - true if $a is equal to z* (literal matching)
[ $a == z* ] - file globbing and word splitting take place
< - less than, in ASCII alphabetical order, if [[ "$a" < "$b" ]], if [ "$a" < "$b" ]
> - greater than, in ASCII alphabetical order, if [[ "$a" > "$b" ]], if [ "$a" > "$b" ]

Useful terminal commands

man

man <command> - show command manual, exit with q

curl

curl -o <filename> -k <url> - download file from url

grep

grep <text> - find lines containing <text>
grep ^<text> - find <text> at the start of a line
grep <text>$ - find <text> at the end of a line
grep <text>. - find line with <text> and exactly one character immediately after it
grep <text>.? - find line with <text> and one optional character immediately after it
grep <text>Z+ - find line with <text> and one or more Z characters immediately after it
grep <text>* - find line with <text> and any number of characters immediately after it

sort

sort -u - strip duplicate lines

tr

echo "some.string.with.dots" | tr . _ - replace dots with underscores

awk

cat <some-file> | awk '{print $2}' - get second word from each line

file

file image_file.jpg - file information including image resolution etc

Oneliners

Run remote script

curl -L https://raw.githubusercontent.com/dummyuser/great-repo/master/install.sh | bash

Command result if

[ $(id -u) -eq 0 ] && return $TRUE || return $FALSE

Var is number

[ "$VAR" =~ ^[0-9]+$ ]

Remove suff/pre-fix

foo=${string#"$prefix"}
foo=${foo%"$suffix"}

Format output into columns

echo <tbd> | column

Schedule command

<cmd> | at <time> - run command at scheduled time

Remove trailing carriage return

echo "<some-text-with-newline>" | tr -d '\r'

Colorized output

Output color codes
tput setaf 1; echo WARNING MESSAGE; tput sgr0;

Code snippets

Argument handling

Git

Code conflict resolution command line manual
git reset --soft - cancel commits, keep changes
git branch -m new-name - rename local branch
git branch -d <branch_name> - delete local branch
git commit --amend - add to previous commit
git remote prune origin - remove merged branches

Terminal shortcuts

ctrl+u - stash command
ctrl+y - pop stash
ctrl+l - clear

About

💾 Bash oneliners and terminal tricks