Skip to content

Instantly share code, notes, and snippets.

@danielkraic
Created January 26, 2017 14:07
Show Gist options
  • Save danielkraic/2ed28fbd726333a21386f6f65aaa1efd to your computer and use it in GitHub Desktop.
Save danielkraic/2ed28fbd726333a21386f6f65aaa1efd to your computer and use it in GitHub Desktop.

bash

filename metacharacters

  • * any string
  • ? any single character
  • [abc..] match one of enclosed characters
  • [!abc..] match any characters not enclosed
  • ~ home dir
  • ~user user home dir
  • ~+ $PWD
  • ~- $OLDPWD

brace expansion

  • pre{A,B,C,..}post
    • hi{A,BB,CCC,DDDD}ha
  • pre{start..end[..incr]}post
    • pre{1..10}post
    • pre{01..10}post
    • pre{1..10..3}post

command forms

cmd & # Execute cmd in background.
cmd1 ; cmd2 # Command sequence; execute multiple cmds on the same line.
{ cmd1 ; cmd2 ; } # Execute commands as a group in the current shell.
(cmd1 ; cmd2) # Execute commands as a group in a subshell.
cmd1 | cmd2 # Pipe; use output from cmd1 as input to cmd2.
cmd1 `cmd2` # Command substitution; use cmd2 output as arguments to cmd1.
cmd1 $(cmd2) # POSIX shell command substitution; nesting is allowed.
cmd $((expression)) # POSIX shell arithmetic substitution.
cmd1 && cmd2 # AND; execute cmd1 and then (if cmd1 succeeds) cmd2.
cmd1 || cmd2 # OR; execute either cmd1 or (if cmd1 fails) cmd2.
! cmd # NOT; execute cmd, and produce a zero exit status if cmd exits with a nonzero status.

redirection forms

  • 0 - stdin
  • 1 - stdout
  • 2 - stderr
cmd > file # Send output of cmd to file (overwrite).
cmd >> file # Send output of cmd to file (append).

cmd < file # Take input for cmd from file.
cmd << text # The contents of the shell script up to a line identical to text become the standard input for cmd

cmd >&n # Send cmd output to file descriptor n.
cmd m>&n # Same as previous, send output from m to n.
cmd >&- # Close standard output.

cmd 2> file # Send standard error to file; standard output remains the same
cmd > file 2>&1 # Send both standard output and standard error to file.
cmd >& file # Same as previous.
cmd &> file # Same as previous. Preferred form.

cmd &>> file # Append both standard output and standard error to file.
cmd > filea 2> fileb # Send standard output to file filea and standard error to file fileb.
cmd | tee files # Send output of cmd to standard output (usually the terminal) and to files.
cmd 2>&1 | tee files # Send standard output and error output of cmd through a pipe to tee to standard output (usually the terminal) and to files.
cmd |& tee files # Same as previous.

cmd <(command) # Run command with its output connected to a named pipe
  diff -u <(sort file1) <(sort file2)
cmd >(command) # Run command with its input connected to a named pipe

functions

function name [()] { body } [redirections]
# fatal -- print error message and die
function fatal {
    echo "$0: fatal error:" "$@" >&2
    exit 1
}

variables

# string
i=5+3; echo $i # will print 5+3
str="hello"
str+=" world"
# integer
declare -i il i=5+3; echo $i # will print 8
# array
declare -a arr
arr=(a b)
echo ${arr[*]}
echo ${arr[0]}
echo ${arr[-2]}

arr+=(c d)

message=([0]=hi [2]=there)

${name[i]} # Use element i of array name; can be any arithmetic expression
${name} # Use element 0 of array name
${name[*]}, ${name[@]} # Use all elements of array name
${#name[*]}, ${#name[@]} # Use the number of elements in array name
# associative arrays
declare -A names
data=([joe]=30 [mary]=25) # Associative array assignment
echo ${data[joe]}
echo ${data[mary]

history

# fc - find command | fix command
fc -l -5
fc -l 10 20

event designators

|!|Begin a history substitution| |!!|Previous command| |!n|Command number n in history list| |!-n|n-th command back from current command| |!string|Most recent command that starts with string| |!?string[?]|Most recent command that contains string| |^old^new^|Quick substitution; change string old to new in previous command|

jobs

|bg|Put the current job in the background| |fg|Put the current job in the foreground| |jobs|List active jobs| |wait|Wait for backgroud jobs to finish| |CTRL Z|Suspend a foreground job|

Built-In Commands

|!|Invert the sense of the following pipeline|if ! who | grep jane > /dev/null; then echo "jane is not logged"; fi| |#|Introduce a comment that runs to the end of the line|| |#!shell|Invoke the named interpreter to execute the script|| |:|Do-nothing command - null command. Returns an exit status of 0|u=daniel; if ! who | grep "$u" > /dev/null; then echo "$u is not logged"; else echo "$u is logged"; fi| |.|Read and execute a file within the current shell|| |[[ ]]|Extended version of the test command|| |name ( )|Define a shell function (POSIX syntax)||name () { commands; } [redirections]| |alias [-p] name=cmd|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment