0% found this document useful (0 votes)
56 views17 pages

Shellmagic PDF

Uploaded by

pjenazabrijanje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views17 pages

Shellmagic PDF

Uploaded by

pjenazabrijanje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.

xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview

File Test Operators


Testing files in scripts is easy and straight forward. This is where
shell scripting starts to show its glory! In Bash you can do file
testing for permissions, size, date, filetype or existence.

Flag Description

File exists

File exists (identical to -e but is deprecated and outdated)

File is a regular file (not a directory or device file)

file is not zero size

file is a directory

file is a block device

file is a character device

file is a pipe

file is a symbolic link

file is a symbolic link

file is a socket

file (descriptor) is associated with a terminal device; this test option may
be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script
is a terminal

file has read permission (for the user running the test)

file has write permission (for the user running the test)

file has execute permission (for the user running the test)

1 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Flag Description

set-user-id (suid) flag set on file.

sticky bit set.

you are owner of file

group-id of file same as yours

file modified since it was last read

file f1 is newer than f2

file f1 is older than f2

files f1 and f2 are hard links to the same file

Not -- reverses the sense of the tests above (returns true if condition
absent).

Job Identifiers
Job control allows you to selectively stop (suspend) the execution
of processes and continue their execution at a later point in time.

Notation Description

Job number [N]

Invocation (command-line) of job begins with string S

Invocation (command-line) of job contains within it string S

"current" job (last job stopped in foreground or started in background)

"current" job (last job stopped in foreground or started in background)

Last job

Last background process

Signals
UNIX System V Signals.

Name Number Action Description


2 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Name Number Action Description

1 exit Hangs up

2 exit Interrupts.

3 core dump Quits.

4 core dump Illegal instruction.

5 core dump Trace trap.

6 core dump IOT instruction.

7 core dump MT instruction.

8 core dump Floating point exception.

Kills (cannot be caught or


9 exit
ignored).

10 core dump Bus error.

11 core dump Segmentation violation.

Bad argument to system


12 core dump
call.

Writes on a pipe with no


13 exit
one to read it.

14 exit Alarm clock.

Software termination
15 exit
signal.

Integer Comparison Operators


How to compare integers or arithmetic expressions in shell scripts.

Flag Description

is equal to

is not equal to

is greater than

is greater than or equal to

3 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Flag Description

is less than

is less than or equal to

is less than (within double parentheses, i.e. )

is less than or equal to (same rule as before)

is greater than (same rule as before)

is greater than or equal to (same rule as before)

String Comparison Operators


How to compare strings in Bash.

Flag Description

is equal to

is equal to (synonymous with the above )

is not equal to

is less than ASCII alphabetical order

is greater than ASCII alphabetical order

string is null (i.e. zero length)

string is not null (i.e. !zero length)

Compound Operators
Useful for boolean expressions and is similar to and . The
compound operators work with the command or may occur within
single brackets

Flag Description

logical and

logical or

4 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview

List Constructs
Provides a means of processing commands consecutively, alas
effectively being able to replace complex if/then/case structures.

Construct Description

and construct

or construct

Reserved Exit Codes


Useful for debugging a script.

Exit Code
Description
No.

Catchall for general errors

Misuse of shell builtins

Command invoked cannot execute

Command not found

Invalid argument to exit

Fatal error signal "n"

Script terminated by Control-C

Exit status out of range

Sending Control Signals


You can use these key-combinations to send signals

Key Combo Description

The interrupt signal, sends SIGINT to the job running in the foreground.

The delayed suspend character. Causes a running process to be stopped when

5 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Key Combo Description

user can foreground, background or kill the process. Delayed suspend is only
available on operating systems supporting this feature.

The suspend signal, sends a SIGTSTP to a running program, thus stopping it


and returning control to the shell.

Check your stty settings. Suspend and resume of output is usually


disabled if you are using "modern" terminal emulations. The standard
xterm supports Ctrl+S and Ctrl+Q by default.

String Manipulation
Bash supports a suprisingly big number of string operations!

Pattern Description

Find the length of the string

Remove from shortest rear (end)


pattern

Remove from longest rear (end)


pattern

Extract substring from $var at


$position

Substring

Remove from shortest front


pattern

Remove from longest front


pattern

Find and replace (only replace


first occurrence)

Find and replace all occurrences

Expands to the names of


variables whose names begin
with prefix.
6 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
GlobbingExpressions
BuiltinsOverview
Pattern Description

Convert first character to


lowercase.

Convert all characters to


lowercase.

Convert first character to


uppercase.

Convert all character to


uppercase.

Replace first match of $substring


with $replacement

Replace all matches of


$substring with $replacement

If $substring matches front end


of $string, substitute
$replacement for $substring

If $substring matches back end


of $string, substitute
$replacement for $substring

Length of matching $substring*


at beginning of $string

Length of matching $substring*


at beginning of $string

Numerical position in $string of


first character in $substring* that
matches [0 if no match, first
character counts as position 1]

Extract $length characters from


$string starting at $position [0 if
no match, first character counts
as position 1]

Extract $substring*, searching


from beginning of $string

7 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
GlobbingExpressions
BuiltinsOverview
Pattern Description

Extract $substring* , searching


from beginning of $string

Extract $substring*, searching


from end of $string

Extract $substring*, searching


from end of $string

Variable Operations
Perform operations on variables.

Expression

Get default shell variables value

Set default shell variables value

Display an error message if parameter is not set

File Types
This is very different from Windows but straightforward once you get
it. You may also have heard the expression "everything is a file in
Linux".

Symbol Meaning

Regular file

Directory

Link

Special file

8 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Symbol Meaning

Named pipe

Block device

Access Codes & Permissions


Now you may know what that arcane looking string ( ) is
when you invoke

Code Description

The access right that is supposed to be on this place is not granted.

read access is granted to the user category defined in this place

write permission is granted to the user category defined in this place

execute permission is granted to the user category defined in this place

user permissions

group permissions

owner permissions

Command Parameters
Command parameters, also known as arguments, are used when invoking
a Bash script.

Command Description

Name of the script itself

Parameter 1 ... 9

9 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Command Description

${10} Positional parameter 10

Expands to the positional parameters, starting from one. When the


expansion occurs within double quotes, it expands to a single word with
the value of each parameter seperated by the first of the IFS environment
variable

Current options

The underscore variable is set at shell startup and contains the absolute
file name of the shell or script being executed as passed in the argument
list. Subsequently, it expands to the last argument to the previous
command, after expansion. It is also set to the full pathname of each
command executed and placed in the environment exported to that
command. When checking mail, this parameter holds the name of the mail
file.

Process id of the shell

Exit status of the most recently executed command

All arguments as separate words

Number of arguments

PID of most recently backgrounded process

History Expansion
Enables use and manipulation of previous commands.

Command Description

Starts a history substitution

Refers to the last command.

Refers to the <n>-th command line.

Refers to the current command line minus <n>.

Refers to the most recent command starting with <string>

Refers to the most recent command containing <string> (the


ending ? is optional)
10 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Command Description

Quick substitution. Repeats the last command, replacing


<string1> with <string2>.

Refers to the entire command line typed so far.

Bash Globbing
Bash cannot recognize RegEx but understand globbing. Globbing is
done to filenames by the shell while RegEx is used for searching
text.

Glob Description

Matches zero or more occurences of a given pattern

Matches zero or one occurences of a given pattern

Matches one or more occurences of a given pattern

Matches exactly one of a given pattern

Negates any pattern — reverses the pattern so to speak

A character class is a set of predefined patterns and comprpised of


the following which:

Regular Expressions
Always use quotes in your RegEx to avoid globbing

11 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Operator Effect

Matches any single character.

The preceding item is optional and will be matched, at most, once.

The preceding item will be matched zero or more times.

The preceding item will be matched one or more times.

The preceding item is matched exactly N times.

The preceding item is matched N or more times.

The preceding item is matched at least N times, but not more than M
times.

Represents the range if it's not first or last in a list or the ending point of
a range in a list.

Matches the empty string at the beginning of a line; also represents the
characters not in the range of a list.

Matches the empty string at the end of a line.

Matches any 1 character from the list.

Matches any 1 character, not in the list!

Matches any 1 character in the range a-f

In basic regular expressions the metacharacters


lose their special meaning;
instead use the backslash versions . Check in your
system documentation whether commands using regular expressions
support extended expressions.

Shell Builtins
Shell builins are built into Bash are often very (if not extremely)
fast compared to external programs. Some of the builtins are
inherited from the Bourne Shell ( ) — these inherited commands
will also work in the original Bourne Shell.

12 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Builtin Description

Reads and executes commands from a designated file in the current shell.

Defines an alias for the specified command.

Resumes a job in background mode.

Binds a keyboard sequence to a read line function or macro.

Exits from a for, while, select, or until loop.

Executes the specified shell built-in command.

Changes the current directory to the specified directory.

Executes the specified command without the normal shell lookup.

Generates possible completion matches for the specified word.

Displays how the specified words would be completed.

Resumes the next iteration of a for, while, select, or until loop.

Declares a variable or variable type.

Displays a list of currently remembered directories.

Removes the specified jobs from the jobs table for the process.

Displays the specified string to STDOUT.

Enables or disables the specified built-in shell command.

Concatenates the specified arguments into a single command, and


executes the command.

Replaces the shell process with the specified command.

Forces the shell to exit with the specified exit status.

Sets the specified variables to be available for child shell processes.

Selects a list of commands from the history list.

Resumes a job in foreground mode.

Parses the specified positional parameters.

13 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Builtin Description

Displays a help file.

Displays the command history.

Builtin Description

Lists active jobs.

Sends a system signal to the specified process ID (PID).

Evaluates each argument in a mathematical expression.

Creates a limited-scope variable in a function.

Exits a login shell.

Removes entries from the directory stack.

Displays text using formatted strings.

Adds a directory to the directory stack.

Displays the pathname of the current working directory.

Reads one line of data from STDIN, and assigns it to a variable.

Reads one line of data from STDIN, and assigns it to a variable that can’t be
changed.

Forces a function to exit with a value that can be retrieved by the calling
script.

Sets and displays environment variable values and shell attributes.

Rotates positional parameters down one position.

Toggles the values of variables controlling optional shell behavior.

Reads and executes commands from a designated file in the current shell.

Suspends the execution of the shell until a SIGCONT signal is received.

Returns an exit status of 0 or 1 based on the specified condition.

Displays the accumulated user and system shell time.

Executes the specified command if the specified system signal is received.

Displays how the specified words would be interpreted if used as a


command.
14 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Builtin Description

Declares a variable or variable type.

Sets a limit on the specific resource for system users.

Sets default permissions for newly created files and directories.

Removes specified alias.

Removes the specified environment variable or shell attribute.

Waits for the specified process to complete, and returns the exit status.

Overview Of Bash Symbols


Here we have gathered a collection of all arcane syntax along with a
brief description. A bunch of these symbols are repeated from
earlier but many are new - this is a good starting point if you are
new to the language.

Symbol Quick Reference

used for comments

used for parameters and variables. Has a bunch of edge cases.

is used for running commands in a subshell.

is used for saving output of commands that are send to run in a subshell.

is used for arithmetic.

is used for saving the output of arithmetic.

deprecated integer expansion construct which is replaced by .


Evaluates integers between the square brackets

is used for testing and is a built-in. Is useful in some cases for filename
expansion and string manipulation.

is used for testing. This is the one you should use unless you can think of a
reason not to.

15 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Symbol Quick Reference

Used for process substitution and is similar to a pipe. Can be used whenever a
command expects a file and you can use multiple at once.

is used for expansion of sequences

is used for variable interpolation and string manipulation.

is a pipe which is used for chaining commands together.

used for feeding input to commands from a file

used for sending output to a file and erasing any previous content in that file.

Symbol Quick Reference

logical or

logical and

used for option prefixes

used for the long-option prefixes

used to send a job to the background

is used for heredocs

is used for herestrings

is used to append output to a file.

single quotes are used to preserve the literal value

double quotes are used to preserve the literal value of all characters
except , and

backslash is used to escape otherwise interpreted symbols/characters


which has a special meaning

used for seperating the components of a filename

similar to a NOP – a do nothing operation. It is a shell builtin with an exit


status of true

used to seperate commands intended to run sequentally.

16 of 17 2/14/20, 9:06 AM
Shell Magic - The Ultimate Bash Cheatsheet! https://shellmagic.xyz/

File Integer String CompoundString Command History Bash Regular Shell Symbol
Testing
Comparison
Comparison
Operators Manipulation
ParametersExpansion
Globbing
Expressions
BuiltinsOverview
Symbol Quick Reference

used for linking together arithmetic operations. All are evalutated but only
the last is returned

expands to the current directory.

expands to parent directory.

expands to home directory.

is deprecated and should not be used. Read further in its respective


section.

If you find this site helpful, please consider sharing it with a


friend!
The more people start using the site, the faster improvements will
come along 

Register for our monthly newsletter!

[email protected] SEND

     

©2020 | Suggest an Enhancement or Contribute to The Site | View our Privacy Policy

contributions welcome stars 38 last commit today

17 of 17 2/14/20, 9:06 AM

You might also like