Created
October 13, 2017 22:06
-
-
Save waqasilyas/7b7c9ce58aa8d086dee5f7a77acee4b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################# Bash CheatSheet Printable ############################### | |
# | |
# Based on https://gist.github.com/LeCoupa/122b12050f5fb267e75f | |
# Removed the more common ones, and added some examples. Also reformatted for easier printing. | |
# 0 Shortcuts. | |
CTRL+A # move to beginning of line | |
CTRL+B # moves backward one character | |
ALT+B # moves backward one word | |
CTRL+C # halts the current command | |
CTRL+D # deletes one character backward or logs out of current session, similar to exit | |
CTRL+E # moves to end of line | |
CTRL+F # moves forward one character | |
ALT+F # moves forward one word | |
CTRL+G # aborts the current editing command and ring the terminal bell | |
CTRL+J # same as RETURN | |
CTRL+K # deletes (kill) forward to end of line | |
CTRL+L # clears screen and redisplay the line | |
CTRL+M # same as RETURN | |
CTRL+N # next line in command history | |
CTRL+O # same as RETURN, then displays next line in history file | |
CTRL+P # previous line in command history | |
CTRL+R # searches backward | |
CTRL+S # searches forward | |
CTRL+T # transposes two characters | |
CTRL+U # kills backward from point to the beginning of line | |
CTRL+V # makes the next character typed verbatim | |
CTRL+W # kills the word behind the cursor | |
CTRL+X # lists the possible filename completeions of the current word | |
CTRL+Y # retrieves (yank) last item killed | |
CTRL+Z # stops the current command, resume with fg in the foreground or bg in the background | |
# 1 File Commands. | |
ls # lists your files | |
ln -s <filename> <link> # creates symbolic link to file | |
touch <filename> # creates or updates your file | |
cat > <filename> # places standard input into file | |
more <filename> # shows the first part of a file (move with space and type q to quit) | |
head <filename> # outputs the first 10 lines of file | |
tail <filename> # outputs the last 10 lines of file (useful with -f option) | |
mv <filename1> <filename2> # moves a file | |
cp <filename1> <filename2> # copies a file | |
rm <filename> # removes a file | |
diff <filename1> <filename2> # compares files, and shows where they differ | |
wc <filename> # tells you how many lines, words and characters there are in a file | |
chmod -options <filename> # lets you change the read, write, and execute permissions on your files | |
gzip <filename> # compresses files | |
gunzip <filename> # uncompresses files compressed by gzip | |
gzcat <filename> # lets you look at gzipped file without actually having to gunzip it | |
grep <pattern> <filenames> # looks for the string in the files | |
grep -r <pattern> <dir> # search recursively for pattern in directory | |
# 2 Variables. | |
varname=value # defines a variable | |
varname=value command # defines a variable to be in the environment of a particular subprocess | |
echo $varname # checks a variable's value | |
export VARNAME=value # defines an environment variable (will be available in subprocesses) | |
$0 $1 $2 $3... # refer to arguments (also in functions) by position; $0 is script itself | |
$@ $* $# # $@ or $* is equal to all parameters. $# holds the number of parameters | |
$- # $- option given to shell | |
$? # exit status of the last command | |
$$ # process id of shell running this script | |
array[0] = val # several ways to define an array | |
array[1] = val | |
array[2] = val | |
array=([2]=val [0]=val [1]=val) | |
array(val val val) | |
${array[i]} # displays array's value for this index. If no index is supplied, array element 0 is assumed | |
${#array[i]} # to find out the length of any element in the array | |
${#array[@]} # to find out how many values there are in the array | |
declare -a # the variables are treaded as arrays | |
declare -f # uses function names only | |
declare -F # displays function names without definitions | |
declare -i # the variables are treaded as integers | |
declare -r # makes the variables read-only | |
declare -x # marks the variables for export via the environment | |
${varname:-word} # if varname exists and not null, return its value; else return word | |
${varname:=word} # if varname exists and not null, return its value; else set it word and return value | |
${varname:?message} # if varname exists and not null, return its value; else print varname+message, and abort | |
${varname:+word} # if varname exists and isn't null, return word; otherwise return null | |
${varname:offset:length} # performs substring expansion and returns result | |
${variable#pattern} # if pattern matches beginning of variable, delete shortest matched part and return the rest | |
${variable##pattern} # if pattern matches beginning of variable, delete longest matched part and return the rest | |
${variable%pattern} # if pattern matches end of variable, delete shortest matched part and return the rest | |
${variable%%pattern} # if pattern matches end of variable, delete longest matched part and return the rest | |
${variable/pattern/string} # longest (only first) match to pattern in variable is replaced by string | |
${variable//pattern/string} # longest (all) matches to pattern in variable is replaced by string | |
${#varname} # returns the length of the value of the variable as a character string | |
*(patternlist) # matches zero or more occurences of the given patterns | |
+(patternlist) # matches one or more occurences of the given patterns | |
?(patternlist) # matches zero or one occurence of the given patterns | |
@(patternlist) # matches exactly one of the given patterns | |
!(patternlist) # matches anything except one of the given patterns | |
$(UNIX command) # command substitution: runs the command and returns standard output | |
# 3 Functions. | |
functname() { | |
shell commands | |
} | |
unset -f functname # deletes a function definition | |
declare -f # displays all defined functions in your login session | |
# 4 Flow Control. | |
set -e # Exit as soon as any line in the bash script fails | |
set -x # Prints each command executed (prefix with ++) | |
set -ex # Do both (for good practice) | |
statement1 && statement2 # and operator, if [[ cond1 ]] && [[ cond2 ]] | |
statement1 || statement2 # or operator | |
-a # and operator inside a test conditional expression | |
-o # or operator inside a test conditional expression; if [ $num -lt 10 -o $num -gt 100 ] | |
! # not operator | |
str1=str2 # str1 matches str2; example: if [ $myvar = "hello" ]; wildcard: if [ $myvar = hello* ] | |
str1!=str2 # str1 does not match str2 | |
str1<str2 # str1 is less than str2 | |
str1>str2 # str1 is greater than str2 | |
-n str1 # str1 is not null (has length greater than 0) | |
-z str1 # str1 is null (has length 0) | |
-a file # file exists; example: if [ -s file ] | |
-d file # file exists and is a directory | |
-e file # file exists; same -a | |
-f file # file exists and is a regular file (i.e., not a directory or other special type of file) | |
-r file # you have read permission | |
-s file # file exists and is not empty | |
-w file # your have write permission | |
-x file # you have execute permission on file, or directory search permission if it is a directory | |
-N file # file was modified since it was last read | |
-O file # you own file | |
-G file # file's group ID matches yours (or one of yours, if you are in multiple groups) | |
file1 -nt file2 # file1 is newer than file2 | |
file1 -ot file2 # file1 is older than file2 | |
-lt # less than; example: if [ $# -gt 1 ] | |
-le # less than or equal | |
-eq # equal | |
-ge # greater than or equal | |
-gt # greater than | |
-ne # not equal | |
if condition; then | |
statements | |
[elif condition; then | |
statements...] | |
[else | |
statements] | |
fi | |
for x := 1 to 10 do | |
begin | |
statements | |
end | |
for name [in list] | |
do | |
statements that can use $name | |
done | |
for (( initialisation ; ending condition ; update )) | |
do | |
statements... | |
done | |
case expression in | |
pattern1 ) | |
statements ;; | |
pattern2 ) | |
statements ;; | |
... | |
esac | |
select name [in list] | |
do | |
statements that can use $name | |
done | |
while condition; do | |
statements | |
done | |
until condition; do | |
statements | |
done | |
# 3 Input/Output Redirectors. | |
cmd1|cmd2 # pipe; takes standard output of cmd1 as standard input to cmd2 | |
< file # takes standard input from file | |
<<label # here-document (content from “label” to “label” | |
<any_loop_or_command> < /file/for/input; <any_loop_or_command> << EOF ... content ... EOF | |
> file # directs standard output to file | |
>> file # directs standard output to file; append to file if it already exists | |
>|file # forces standard output to file even if noclobber is set | |
n>|file # forces output to file from file descriptor n even if noclobber is set | |
<> file # uses file as both standard input and standard output | |
n<>file # uses file as both input and output for file descriptor n | |
n>file # directs file descriptor n to file | |
n<file # takes file descriptor n from file | |
n>>file # directs file description n to file; append to file if it already exists | |
n>& # duplicates standard output to file descriptor n | |
n<& # duplicates standard input from file descriptor n | |
n>&m # file descriptor n is made to be a copy of the output file descriptor | |
n<&m # file descriptor n is made to be a copy of the input file descriptor | |
&>file # directs standard output and standard error to file | |
<&- # closes the standard input | |
>&- # closes the standard output | |
n>&- # closes the ouput from file descriptor n | |
n<&- # closes the input from file descripor n | |
# 4 Tips | |
# To suspend a job, type CTRL+Z while it is running. You can also suspend a job with CTRL+Y. | |
# This is slightly different from CTRL+Z in that the process is only stopped when it attempts to read input from terminal. | |
# Of course, to interupt a job, type CTRL+C. | |
myCommand & # runs job in the background and prompts back the shell | |
jobs # lists all jobs (use with -l to see associated PID) | |
fg # brings a background job into the foreground | |
fg %+ # brings most recently invoked background job | |
fg %string # brings job whose command begins with string | |
fg %?string # brings job whose command contains string | |
kill -l # returns a list of all signals on the system, by name and number | |
kill PID # terminates process with specified PID | |
ps # prints information about the current running login shell and any processes running under it | |
ps -a # selects all processes with a tty except session leaders | |
disown <PID|JID> # removes the process from the list of jobs | |
wait # waits until all background jobs have finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment