Bash commands cheatsheet
String case manipulations:
VAR="Hello"
# Print uppercase
echo ${VAR^^}
HELLO
# Print lowercase
echo ${VAR,,}
hello
# Print reverse
echo ${VAR~~}
hELLOIf 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/executablePrint part of string:
Fix command typos without the need of writing the whole parameters
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
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:
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:
To revert the file from httpd.conf.bak to httpd.conf:
To rename the file with prefix “old”:
Default values
services
Last updated