foo=barfor setting variables, access with$like$foo- No space between
=and variable/expression!
- No space between
- Strings defined with
''will not substitute variables, but""will:
echo "$foo" # bar
echo '$foo' # $foo - In addition to program output via
stdoutandstderr, programs give a return code - 0 means good, != 0 means an error - Important variables:
$0= name of the script$1to$9= arguments to the script.$1is the first argument, etc.$@= all arguments$#= number of arguments$?= return code of the previous command$$= process identification number (PID) for the current script (?)!!= entire last command, including arguments- If a command fails due to missing permissions, quickly re-execute it with sudo by doing
sudo !!
- If a command fails due to missing permissions, quickly re-execute it with sudo by doing
$_= last argument from last command.alt+.does the same thing
- Logical operators: allows exit codes to be used to conditionally run commands
&&= “and” operator, e.g.COMMAND_1 && COMMAND_2||= “or” operator, e.g.COMMAND_1 || COMMAND_2;= same-line command call, no dependency, e.g.COMMAND_1 ; COMMAND_2&&and||are short-circuiting, so not all commands will be run if the outcome is determined partway through. These can be combined with true or false, which are programs that always return 0 and 1, respectively
$(COMMAND): command substitution, aka output ofCOMMANDis substituted in- Allows you to set variables equal to output, e.g.
foo=$(COMMAND)
- Allows you to set variables equal to output, e.g.
<(COMMAND): process substitution, aka writes command output to temporary file and substitutes file name in, for when a file input is expected
- Shell globbing: using expressions to express multiple values (often command inputs) efficiently
?= represents any one character, e.g.foo?→foo1 foo2 fool, but notfoo10*= represents any amount of characters{}=image.{png,jpg}→image.png image.jpg,{a..e}→a b c d e,{1..5}→1 2 3 4 5
- Shebang: allows scripts of different types to be run by the shell via
./script{.sh,.py,...}- Bash:
#!/bin/bash - Python:
#!/usr/bin/env python(superior to#!/usr/local/bin/pythonin terms of portability)
- Bash:
shellcheck SCRIPThelps with debuggingsudo apt install shellcheck
- Shell functions versus scripts
- Functions must be in the same language as the shell
- Functions are loaded once read, scripts are loaded every time they run
- Functions that are executed will execute the commands in your current shell environment, while running scripts run them in a separate process, i.e.
cdwill not change your working directory in a script, but will in a function
./SCRIPTvssource SCRIPT: running a script with./SCRIPTwill run it in a new instance of bash, so commands likecdwill not affect the working directory in the session, butsource SCRIPTwill run in the current session- Bash scripts are good for simple one-off scripts/a series of commands, Python scripts are good for larger/complex processes
tldr PROGRAM: gives succinct documentation with examples (web client: https://tldr.inbrowser.app/)find DIR -name "SEARCH_STRING" -type {d,f}: searches recursively insideDIRfor directory/file that matches search string- Can also search by size, owner, permissions, modified time, etc.
- Adding
-exec COMMAND {} \;will execute command with{}representing thefindoutput becomingCOMMANDinput fdis an improved alternative: https://github.com/sharkdp/fd#installation
locate "SEARCH_STRING": faster by using a database, which is updated usingupdatedb- Can only search by string, and database is not as fresh
grep "SEARCH_STRING" FILES: search inside files
grep -R "SEARCH_STRING": recursive directory search
COMMAND | grep "SEARCH_STRING": search lines of output from commandCOMMAND | fzf: fromfzfpackage, allows for an interactive, fuzzy search- e.g.
find DIR | fzf
- e.g.
rg: aka “ripgrep” is agrepalternative with other options to usefd,ag,ack, are all similargrepalternatives
- Previous command search
history NUMBER= spit out the lastNUMBERcommands in history, can use with| grepctrl-r= for history search, made more robust withfzf
- Navigation tools
tree: branched visualization of filesystembroot: interactive branched visualization with fuzzy searchnnn: provides file system navigation similar to GUI