/Users/admin/.bash_profile.
- Good Practices
- Basic Shell Script formatting
- Path
- Prompt
- Shortcuts
- Env Vars
# find all files linked to application | |
mdfind -name "bitbar" | |
# remove fully an application | |
sudo rm -rif ~/Directory/Component/Removeme.pane | |
sudo rm -rif /TheLibrary/LaunchDaemons/sketchyd | |
sudo rm -rif /usr/sbin/crudrunner | |
sudo rm -rif ~/Download/sketchydaemon-installer.tgz | |
mdfind -name [application name] | |
sudo rm -rf /Whatever |
Good practices: | |
#!/bin/bash #Explicitly tell shell this is a bash script | |
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) | |
Tests (for ifs and loops) are done with [ ] or with the test command. | |
Checking files: | |
-r file #Check if file is readable. | |
-w file #Check if file is writable. | |
-x file #Check if we have execute access to file. | |
-f file #Check if file is an ordinary file (not a directory, a device file, etc.) | |
-s file #Check if file has size greater than 0. | |
-d file #Check if file is a directory. | |
-e file #Check if file exists. Is true even if file is a directory. | |
Example: | |
if [ -s file ] | |
then | |
#such and such | |
fi | |
Checking strings: | |
s1 = s2 #Check if s1 equals s2. | |
s1 != s2 #Check if s1 is not equal to s2. | |
-z s1 #Check if s1 has size 0. | |
-n s1 #Check if s2 has nonzero size. | |
s1 #Check if s1 is not the empty string. | |
Example: | |
if [ $myvar = "hello" ] ; then | |
echo "We have a match" | |
fi | |
Checking numbers: Note that a shell variable could contain a string that represents a number. If you want to check the numerical value use one of the following: | |
n1 -eq n2 #Check to see if n1 equals n2. | |
n1 -ne n2 #Check to see if n1 is not equal to n2. | |
n1 -lt n2 #Check to see if n1 < n2. | |
n1 -le n2 #Check to see if n1 <= n2. | |
n1 -gt n2 #Check to see if n1 > n2. | |
n1 -ge n2 #Check to see if n1 >= n2. | |
Example: | |
if [ $# -gt 1 ] | |
then | |
echo "ERROR: should have 0 or 1 command-line parameters" | |
fi | |
Boolean operators: | |
! #not | |
-a #and | |
-o #or | |
Example: | |
if [ $num -lt 10 -o $num -gt 100 ] | |
then | |
echo "Number $num is out of range" | |
elif [ ! -w $filename ] | |
then | |
echo "Cannot write to $filename" | |
fi | |
Note that ifs can be nested. For example: | |
if [ $myvar = "y" ] | |
then | |
echo "Enter count of number of items" | |
read num | |
if [ $num -le 0 ] | |
then | |
echo "Invalid count of $num was given" | |
else | |
#... do whatever ... | |
fi | |
fi | |
The above example also illustrates the use of read to read a string from the keyboard and place it into a shell variable. Also note that most UNIX commands return a true (nonzero) or false (0) in the shell variable status to indicate whether they succeeded or not. This return value can be checked. At the command line echo $status. In a shell script use something like this: | |
if grep -q shell bshellref | |
then | |
echo "true" | |
else | |
echo "false" | |
fi | |
Note that -q is the quiet version of grep. It just checks whether it is true that the string shell occurs in the file bshellref. It does not print the matching lines like grep would otherwise do. | |
I/O Redirection: | |
pgm > file #Output of pgm is redirected to file. | |
pgm < file #Program pgm reads its input from file. | |
pgm >> file #Output of pgm is appended to file. | |
pgm1 | pgm2 #Output of pgm1 is piped into pgm2 as the input to pgm2. | |
n > file #Output from stream with descriptor n redirected to file. | |
n >> file #Output from stream with descriptor n appended to file. | |
n >& m #Merge output from stream n with stream m. | |
n <& m #Merge input from stream n with stream m. | |
<< tag #Standard input comes from here through next tag at start of line. | |
Note that file descriptor 0 is normally standard input, 1 is standard output, and 2 is standard error output. | |
Shell Built-in Variables: | |
$0 #Name of this shell script itself. | |
$1 #Value of first command line parameter (similarly $2, $3, etc) | |
$# #In a shell script, the number of command line parameters. | |
$* #All of the command line parameters. | |
$- #Options given to the shell. | |
$? #Return the exit status of the last command. | |
$$ #Process id of script (really id of the shell running the script) | |
Pattern Matching: | |
* #Matches 0 or more characters. | |
? #Matches 1 character. | |
[AaBbCc] #Example: matches any 1 char from the list. | |
[^RGB] #Example: matches any 1 char not in the list. | |
[a-g] #Example: matches any 1 char from this range. | |
Quoting: | |
\c #Take character c literally. | |
`cmd` #Run cmd and replace it in the line of code with its output. | |
"whatever" #Take whatever literally, after first interpreting $, `...`, \ | |
'whatever' #Take whatever absolutely literally. | |
Example: | |
match=`ls *.bak` #Puts names of .bak files into shell variable match. | |
echo \* #Echos * to screen, not all filename as in: echo * | |
echo '$1$2hello' #Writes literally $1$2hello on screen. | |
echo "$1$2hello" #Writes value of parameters 1 and 2 and string hello. | |
Grouping: Parentheses may be used for grouping, but must be preceded by backslashes since parentheses normally have a different meaning to the shell (namely to run a command or commands in a subshell). For example, you might use: | |
if test \( -r $file1 -a -r $file2 \) -o \( -r $1 -a -r $2 \) | |
then | |
#do whatever | |
fi | |
Case statement: Here is an example that looks for a match with one of the characters a, b, c. If $1 fails to match these, it always matches the * case. A case statement can also use more advanced pattern matching. | |
case "$1" in | |
a) cmd1 ;; | |
b) cmd2 ;; | |
c) cmd3 ;; | |
*) cmd4 ;; | |
esac | |
Loops: Bash supports loops written in a number of forms, | |
for arg in [list] | |
do | |
echo $arg | |
done | |
for arg in [list] ; do | |
echo $arg | |
done | |
You can supply [list] directly | |
NUMBERS="1 2 3" | |
for number in `echo $NUMBERS` | |
do | |
echo $number | |
done | |
for number in $NUMBERS | |
do | |
echo -n $number | |
done | |
for number in 1 2 3 | |
do | |
echo -n $number | |
done | |
If [list] is a glob pattern then bash can expand it directly, for example: | |
for file in *.tar.gz | |
do | |
tar -xzf $file | |
done | |
You can also execute statements for [list], for example: | |
for x in `ls -tr *.log` | |
do | |
cat $x >> biglog | |
done | |
Shell Arithmetic: In the original Bourne shell arithmetic is done using the expr command as in: | |
result=`expr $1 + 2` | |
result2=`expr $2 + $1 / 2` | |
result=`expr $2 \* 5` #note the \ on the * symbol | |
With bash, an expression is normally enclosed using [ ] and can use the following operators, in order of precedence: | |
* / % #(times, divide, remainder) | |
+ - #(add, subtract) | |
< > <= >= #(the obvious comparison operators) | |
== != #(equal to, not equal to) | |
&& #(logical and) | |
|| #(logical or) | |
= #(assignment) | |
Arithmetic is done using long integers. Example: | |
result=$[$1 + 3] | |
In this example we take the value of the first parameter, add 3, and place the sum into result. | |
Order of Interpretation: The bash shell carries out its various types of interpretation for each line in the following order: | |
brace expansion (see a reference book) | |
~ expansion (for login ids) | |
parameters (such as $1) | |
variables (such as $var) | |
command substitution (Example: match=`grep DNS *` ) | |
arithmetic (from left to right) | |
word splitting | |
pathname expansion (using *, ?, and [abc] ) | |
Other Shell Features: | |
$var #Value of shell variable var. | |
${var}abc #Example: value of shell variable var with string abc appended. | |
# #At start of line, indicates a comment. | |
var=value #Assign the string value to shell variable var. | |
cmd1 && cmd2 #Run cmd1, then if cmd1 successful run cmd2, otherwise skip. | |
cmd1 || cmd2 #Run cmd1, then if cmd1 not successful run cmd2, otherwise skip. | |
cmd1; cmd2 #Do cmd1 and then cmd2. | |
cmd1 & cmd2 #Do cmd1, start cmd2 without waiting for cmd1 to finish. | |
(cmds) #Run cmds (commands) in a subshell. | |
Update | |
To update Bash-it, simply run: | |
bash-it update | |
that's all. | |
Help Screens | |
bash-it show aliases # shows installed and available aliases | |
bash-it show completions # shows installed and available completions | |
bash-it show plugins # shows installed and available plugins | |
bash-it help aliases # shows help for installed aliases | |
bash-it help completions # shows help for installed completions | |
bash-it help plugins # shows help for installed plugins | |
❯ bash-it search ruby rake gem bundle irb rails | |
aliases: bundler rails | |
plugins: chruby chruby-auto ruby | |
completions: bundler gem rake | |
$ cat launchscript.sh | |
#! /bin/sh | |
sudo service mysql stop sudo | |
/opt/lampp/lampp start | |
sudo /usr/bin/skype start | |
sudo /usr/bin/subl | |
start sudo google-chrome | |
sudo scudcloud | |
sudo bcompare | |
$ cd lighttpd-1.x.x | |
$ ./configure | |
$ make | |
$ su - | |
# make install | |
# exit | |
Update .bash_profile | |
echo 'source ~/.dotfiles/base.sh' >> ~/.bash_profile | |
# Color Variables | |
RED='\\033[0;31m' | |
GREEN='\\033[0;32m' | |
NC='\\033[0m' # No Color | |
# Ask for the administrator password upfront. | |
sudo -v | |
# Keep Sudo Until Script is finished | |
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null & | |
# Check if OSX Command line tools are installed | |
if type xcode-select >&- && xpath=$( xcode-select --print-path ) && | |
test -d "${xpath}" && test -x "${xpath}" ; then | |
############################################################################### | |
# Computer Settings # | |
############################################################################### | |
echo -e "${RED}Enter your computer name please?${NC}" | |
read cpname | |
echo -e "${RED}Please enter your name?${NC}" | |
read name | |
echo -e "${RED}Please enter your git email?${NC}" | |
read email | |
clear | |
sudo scutil --set ComputerName "$cpname" | |
sudo scutil --set HostName "$cpname" | |
sudo scutil --set LocalHostName "$cpname" | |
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "$cpname" | |
echo $PATH | |
echo $BASH_ENV | |
ssh user@host 'echo $PWD' | |
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" | |
#!/bin/sh | |
# Useful aliases | |
alias ls=‘ls $LS_OPTIONS -hF’ | |
alias ll=‘ls $LS_OPTIONS -lhF’ | |
alias l=‘ls $LS_OPTIONS -lAhF’ | |
alias cd..=“cd ..” | |
alias c=“clear” | |
alias e=“exit” | |
alias ssh=“ssh -X” | |
alias ..=“cd ..” | |
# Useful aliases | |
alias ls=‘ls $LS_OPTIONS -hF’ | |
alias ll=‘ls $LS_OPTIONS -lhF’ | |
alias l=‘ls $LS_OPTIONS -lAhF’ | |
alias cd..=“cd ..” | |
alias c=“clear” | |
alias e=“exit” | |
alias ssh=“ssh -X” | |
alias ..=“cd ..” |
## BASH IO | |
#Yes it is possible, just redirect the output to a file: | |
someCommand > someFile.txt | |
#Or if you want to append data: | |
someCommand >> someFile.txt | |
#If you want stderr too use this: | |
someCommand &> someFile.txt | |
#or this to append: | |
someCommand &>> someFile.txt |
#!/bin/sh | |
# ALL THE BELOW WILL BE INSERTED INTO .bash_profile | |
# EXPORT PATHS to $PATH | |
export PATH=/opt/local/bin:/opt/local/sbin:$PATH | |
export MANPATH=/opt/local/share/man:$MANPATH |
#!/bin/sh | |
# setting new escape sequences | |
echo $PS | |
# \\a an ASCII bell character (07) | |
#\\d the date | |
#\\e an ASCII escape character (033) | |
#\\h the hostname up to the first `.' | |
#\\H the hostname | |
#\\j the number of jobs currently managed by the shell | |
#\\l the basename of the shell's terminal device name | |
#\\n newline | |
#\\r carriage return | |
#\\s the name of the shell, the basename of $0 (the portion following the final slash) | |
#\\t the current time in 24-hour HH:MM:SS format | |
#\\T the current time in 12-hour HH:MM:SS format \\@ the current time in 12-hour am/pm format | |
#\\u the username of the current user | |
#\\v the version of bash (e.g., 2.00) | |
#\\V the release of bash, version + patchlevel | |
#\\w the current working directory | |
#\\W the basename of the current working directory | |
#\\! the history number of this command | |
#\\# the command number of this command | |
#\\$ if the effective UID is 0, a #, otherwise a $ | |
#\\nnn the character corresponding to the octalnumber nnn | |
#\\\\ a backslash \\[ begin a sequence of non-printing characters | |
# \\] end a sequence of non-printing character | |
PS1="[\\t][\\u@\\h:\\W]\\$ " | |
[\\t][\\u@\\h:\\W]$ | |
# EXPORTS PROMPT | |
export PS1=“$NM[ $HI\u $HII\h $SI\w$NM ] $IN” | |
# naming tabs | |
echo $PROMPT_COMMAND | |
echo -ne "\\033]0;${PWD/#HOME/~}\\007" | |
export PROMPT_COMMAND='echo -ne "\\033]0;YOUR NAME HERE\\007"' | |
export PROMPT_COMMAND='echo -ne "\\033]0;${PWD/#$HOME/~}\\007"' |
#!/bin/bash | |
##################################################### | |
# Name: Bash CheatSheet for Mac OSX | |
# | |
# A little overlook of the Bash basics | |
# | |
# Usage: | |
# | |
# Author: J. Le Coupanec | |
# Date: 2014/11/04 | |
##################################################### | |
# 0. Shortcuts. | |
CTRL+A # move to beginning of line | |
CTRL+B # moves backward one character | |
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 | |
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 completefions 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 | |
DELETE # deletes one character backward | |
!! # repeats the last command | |
exit # logs out of current session | |
# 1. Bash Basics. | |
export # displays all environment variables | |
echo $SHELL # displays the shell you're using | |
echo $BASH_VERSION # displays bash version | |
bash # if you want to use bash (type exit to go back to your normal shell) | |
whereis bash # finds out where bash is on your system | |
clear # clears content on window (hide displayed lines) | |
# 1.1. File Commands. | |
ls # lists your files | |
ls -l # lists your files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified | |
ls -a # lists all files, including hidden 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) | |
emacs <filename> # lets you create and edit a file | |
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 | |
lpr <filename> # print the file | |
lpq # check out the printer queue | |
lprm <jobnumber> # remove something from the printer queue | |
genscript # converts plain text files into postscript for printing and gives you some options for formatting | |
dvips <filename> # print .dvi files (i.e. files produced by LaTeX) | |
grep <pattern> <filenames> # looks for the string in the files | |
grep -r <pattern> <dir> # search recursively for pattern in directory | |
# 1.2. Directory Commands. | |
mkdir <dirname> # makes a new directory | |
cd # changes to home | |
cd <dirname> # changes directory | |
pwd # tells you where you currently are | |
# 1.3. SSH, System Info & Network Commands. | |
ssh user@host # connects to host as user | |
ssh -p <port> user@host # connects to host on specified port as user | |
ssh-copy-id user@host # adds your ssh key to host for user to enable a keyed or passwordless login | |
whoami # returns your username | |
passwd # lets you change your password | |
quota -v # shows what your disk quota is | |
date # shows the current date and time | |
cal # shows the month's calendar | |
uptime # shows current uptime | |
w # displays whois online | |
finger <user> # displays information about user | |
uname -a # shows kernel information | |
man <command> # shows the manual for specified command | |
df # shows disk usage | |
du <filename> # shows the disk usage of the files and directories in filename (du -s give only a total) | |
last <yourUsername> # lists your last logins | |
ps -u yourusername # lists your processes | |
kill <PID> # kills (ends) the processes with the ID you gave | |
killall <processname> # kill all processes with the name | |
top # displays your currently active processes | |
bg # lists stopped or background jobs ; resume a stopped job in the background | |
fg # brings the most recent job in the foreground | |
fg <job> # brings job to the foreground | |
ping <host> # pings host and outputs results | |
whois <domain> # gets whois information for domain | |
dig <domain> # gets DNS information for domain | |
dig -x <host> # reverses lookup host | |
wget <file> # downloads file | |
# 2. Basic Shell Programming. | |
# 2.1. 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 | |
echo $$ # prints process ID of the current shell | |
echo $! # prints process ID of the most recently invoked background job | |
echo $? # displays the exit status of the last command | |
export VARNAME=value # defines an environment variable (will be available in subprocesses) | |
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 funtion 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 isn't null, return its value; otherwise return word | |
${varname:=word} # if varname exists and isn't null, return its value; otherwise set it word and then return its value | |
${varname:?message} # if varname exists and isn't null, return its value; otherwise print varname, followed by message and abort the current command or script | |
${varname:+word} # if varname exists and isn't null, return word; otherwise return null | |
${varname:offset:length} # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters | |
${variable#pattern} # if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest | |
${variable##pattern} # if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest | |
${variable%pattern} # if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest | |
${variable%%pattern} # if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest | |
${variable/pattern/string} # the longest match to pattern in variable is replaced by string. Only the first match is replaced | |
${variable//pattern/string} # the longest match to pattern in variable is replaced by string. All matches are replaced | |
${#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 | |
# 2.2. Functions. | |
# The function refers to passed arguments by position (as if they were positional parameters), that is, $1, $2, and so forth. | |
# $@ is equal to "$1" "$2"... "$N", where N is the number of positional parameters. $# holds the number of positional parameters. | |
functname() { | |
shell commands | |
} | |
unset -f functname # deletes a function definition | |
declare -f # displays all defined functions in your login session | |
# 2.3. Flow Control. | |
statement1 && statement2 # and operator | |
statement1 || statement2 # or operator | |
-a # and operator inside a test conditional expression | |
-o # or operator inside a test conditional expression | |
str1=str2 # str1 matches str2 | |
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 | |
-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 | |
-r 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 | |
-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. Command-Line Processing Cycle. | |
# The default order for command lookup is functions, followed by built-ins, with scripts and executables last. | |
# There are three built-ins that you can use to override this order: `command`, `builtin` and `enable`. | |
command # removes alias and function lookup. Only built-ins and commands found in the search path are executed | |
builtin # looks up only built-in commands, ignoring functions and commands found in PATH | |
enable # enables and disables shell built-ins | |
eval # takes arguments and run them through the command-line processing steps all over again | |
# 4. Input/Output Redirectors. | |
cmd1|cmd2 # pipe; takes standard output of cmd1 as standard input to cmd2 | |
> file # directs standard output to file | |
< file # takes standard input from 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 | |
<<label # here-document | |
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 | |
# 5. Process Handling. | |
# 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 %- # brings second most recently invoked background job | |
fg %N # brings job number N | |
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 a line of information about the current running login shell and any processes running under it | |
ps -a # selects all processes with a tty except session leaders | |
trap cmd sig1 sig2 # executes a command when a signal is received by the script | |
trap "" sig1 sig2 # ignores that signals | |
trap - sig1 sig2 # resets the action taken when the signal is received to the default | |
disown <PID|JID> # removes the process from the list of jobs | |
wait # waits until all background jobs have finished | |
# 6. Tips and Tricks. | |
# set an alias | |
cd; nano .bash_profile | |
> alias gentlenode='ssh [email protected] -p 3404' # add your alias in .bash_profile | |
# to quickly go to a specific directory | |
cd; nano .bashrc | |
> shopt -s cdable_vars | |
> export websites="/Users/mac/Documents/websites" | |
source .bashrc | |
cd websites | |
# 7. Debugging Shell Programs. | |
bash -n scriptname # don't run commands; check for syntax errors only | |
set -o noexec # alternative (set option in script) | |
bash -v scriptname # echo commands before running them | |
set -o verbose # alternative (set option in script) | |
bash -x scriptname # echo commands after command-line processing | |
set -o xtrace # alternative (set option in script) | |
trap 'echo $varname' EXIT # useful when you want to print out the values of variables at the point that your script exits | |
function errtrap { | |
es=$? | |
echo "ERROR line $1: Command exited with status $es." | |
} | |
trap 'errtrap $LINENO' ERR # is run whenever a command in the surrounding script or function exists with non-zero status | |
function dbgtrap { | |
echo "badvar is $badvar" | |
} | |
trap dbgtrap DEBUG # causes the trap code to be executed before every statement in a function or script | |
# ...section of code in which the problem occurs... | |
trap - DEBUG # turn off the DEBUG trap | |
function returntrap { | |
echo "A return occured" | |
} | |
trap returntrap RETURN # is executed each time a shell function or a script executed with the . or source commands finishes executing | |
#How do I loop through only directories in bash? | |
for d in */ ; do | |
echo "$d" | |
done | |
# copy | |
cp -source -destination | |
# create file | |
touch .bash_profile | |
# list folder contents folder and rights | |
ls -1 | |
# create file | |
# You can specify the file size in bytes (b), kilobytes (k), megabytes (m) or gigabytes (g) | |
mkfile 1g test.abc | |
# open opens files, directories and applications | |
open /Applications/Safari.app/ | |
# copy and paste | |
# These two commands let you copy and paste text from the command line. Of course, you could also just use your mouse—but the real power of pbcopy and pbpaste comes from the fact that they’re UNIX commands, and that means they benefit from piping, redirection, and the ability to be in scripts in conjunction with other commands | |
ls ~ | pbcopy | |
# will copy a list of files in your home directory to the OS X clipboard. | |
pbcopy < blogpost.txt | |
# find file | |
mdfind -onlyin ~/Documents essay | |
# copying large amounts of data as it can run within a Terminal window | |
# Adding -V, meaning verbose prints a line to the Terminal window for every file | |
ditto -V /old/work/ /new/work/ | |
# stress test ma | |
yes | |
# view file system usage | |
sudo fs_usage | |
# View the Contents of Any File | |
cat /path/to/file | |
# Start a Simple HTTP Server in Any Folder | |
python -m SimpleHTTPServer 8000 | |
# Run the Same Command Again | |
!! | |
sudo !! | |
# download file without browser | |
curl -O http://appldnld.apple.com/iTunes11/091-6058.20130605.Cw321/iTunes11.0.4.dmg | |
# Continually Monitor the Output of a File | |
tail -f /var/log/system.log | |
# ip address | |
ipconfig getifaddr en0 # internal | |
curl ipecho.net/plain; echo # external | |
# network connectivity | |
ping -c 10 www.apple.com | |
# active processes | |
top | |
# See A List of All The Commands You’ve Entered | |
$ history | |
# create bash exe | |
chmod +x | |
# screenshot | |
# flags screencapture --help | |
screencapture -C -M image.png | |
screencapture -c -W | |
screencapture -T 10 -P image.png | |
screencapture -s -t pdf image.pdf | |
# launchctl | |
# launchctl lets you interact with the OS X init script system, launchd | |
# Running launchctl list will show you what launch scripts are currently loaded. | |
sudo launchctl load -w | |
# manual | |
# The man command to bring up help manuals isn’t exclusive to OS X, nor is there much that’s new to say about it | |
man | |
# ssh-add for security keys | |
# ssh -i keyfile.pem [server] | |
ssh-add -k keyfile.pem | |
ssh [server] | |
# image processing (sips) | |
# sips is an image processing tool and a native alternative to ImageMagick | |
# http://www.leancrew.com/all-this/2014/05/a-little-sips/ | |
for file in *.jpeg; do sips -s format png $file --out $file.png | |
# manupulate text docs | |
# textutil uses Cocoa’s text engine to manipulate documents and convert them between various formats | |
textutil -convert html article.doc | |
textutil -cat rtf article1.doc article2.doc article3.doc | |
# shows current director | |
pwd |
#!/bin/sh | |
echo $PATH | |
echo $BASH_ENV | |
ssh user@host 'echo $PWD' | |
# non login shll | |
bash | |
# login shell | |
bash myscript.sh | |
# shebang | |
#!/usr/bin/enb bash | |
# init | |
~/ect/profile | |
~/.bash_profile | |
~/.bash_login ~/.profile | |
/etc/bash.bashrc | |
~/.bashrc |