Bash commands cheatsheet

String case manipulations:

VAR="Hello"

# Print uppercase
echo ${VAR^^}
HELLO

# Print lowercase
echo ${VAR,,}
hello

# Print reverse
echo ${VAR~~}
hELLO

If string is set or not:

VAR="/path/to/executable"

echo ${VAR:-/path/to/other/executable}
/path/to/executable

unset VAR
echo ${VAR:-/path/to/other/executable}
/path/to/other/executable

Print part of string:

VAR=`pwd`
echo ${VAR}
/home/user/foo/bar
echo ${VAR##*/}
bar
echo ${VAR%/*}
/home/user/foo

OR

name=foo.mydomain.xyz
echo ${name%%.*}
foo
echo ${name%.*}
foo.mydomain
echo ${name##*.}
xyz

name="John"
echo ${name}
echo ${name/J/j}    #=> "john" (substitution)
echo ${name:0:2}    #=> "Jo" (slicing)
echo ${name::2}     #=> "Jo" (slicing)
echo ${name::-1}    #=> "Joh" (slicing)
echo ${name:(-1)}   #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake}  #=> $food or "Cake"

Fix command typos without the need of writing the whole parameters

sl -la /tmp
-bash: sl: command not found

^sl^ls
ls -la /tmp
total 1168
drwxrwxrwt  11 root               wheel     352 Dec  5 13:52 .
drwxr-xr-x   6 root               wheel     192 Nov 18 19:22 ..
-rw-rw-rw-@  1 root               wheel       0 Dec  3 12:10 .some-file
drwxrwxrwx   2 root               wheel      64 Dec  5 07:43 boost_interprocess
srwxr-xr-x   1 root               wheel       0 Nov 20 22:07 epp-zc23.n501
srwxrw-rw-   1 root               wheel       0 Nov 20 22:06 epp-zc239fd3
drwxr-xr-x   2 root               wheel      64 Dec  2 23:27 powerlog
.....

Bang quite useful, when we want to play with the bash history commands. Bang helps by letting you execute commands in history easily when you need them:

  • !! — Execute the last executed command in the bash history

  • !* — Execute the command with all the arguments passed to the previous command

  • — Get the first argument of the last executed command in the bash history

  • !$ — Get the last argument of the last executed command in the bash history

  • ! — Execute a command which is in the specified number in bash history

  • !?keyword? — Execute a command from bash history for the first pattern match of the specified keyword

  • !-N — Execute the command that was Nth position from the last in bash history

~/bin/lg-backup
sudo !!

In the last part of the above example we didn’t realize that the lg-backup command had to be run with sudo. Now, Instead of typing the whole command again with sudo, we can just use sudo !! which will re-run the last executed command in bash history as sudo, which saves us lot of time.

There are situations where we end up in creating/deleting the directories whose name start with a symbol. These directories can not be removed by just using rm -rf or rmdir. So we need to use the “double dash” (--) to perform deletion of such directories:

mkdir -- -symbol_dir

rm -rf -symbol_dir/
rm: invalid option -- 't'
....

rm -rf -- -symbol_dir
ls -la 
total 0

Comma and Braces Operators

We can do lot with comma and braces to make our life easier when we are performing some operations, lets see few usages:

  • Rename and backup operations with comma & braces operator

  • Pattern matching with comma & braces operator

  • Rename and backup (prefixing name) operations on long file names

To backup httpd.conf to httpd.conf.bak:

cp httpd.conf{,.bak}

To revert the file from httpd.conf.bak to httpd.conf:

mv http.conf{.bak,}

To rename the file with prefix “old”:

cp exampleFile old-!#^

ls -la 
total 2
drwxrwxrwt  14 root               wheel     448 Dec  5 15:13 .
drwxr-xr-x   6 root               wheel     192 Nov 18 19:22 ..
drwxr-xr-x   1 root               wheel     192 Nov 18 19:22 exampleFile
drwxr-xr-x   1 root               wheel     192 Nov 18 19:22 old-exampleFile

Default values

${FOO:-val}	$FOO, or val if not set
${FOO:=val}	Set $FOO to val if not set
${FOO:+val}	val if $FOO is set
${FOO:?message}	Show error message and exit if $FOO is not set

services

service --status-all

#or 

systemctl list-unit-files

Last updated

Was this helpful?