Skip to content
Tak Tran edited this page Jul 29, 2019 · 8 revisions

Start of bash scripts

#!/bin/bash
set -euxo pipefail

https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/

Standard out/error

Only show errors (ie, send standard out to /dev/null)

[cmd] > /dev/null

Only output (ie, send standard error to /dev/null)

[cmd] 2> /dev/null

xargs

xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

https://neverendingsecurity.wordpress.com/2015/04/13/xargs-commands-cheatsheet/

Use -t to print the command being run (https://shapeshed.com/unix-xargs/)

echo 'one two three' | xargs -t rm
rm one two three

Use -L1 to trigger the command for every line

$ echo one\ntwo\nthree | xargs echo "pre"
pre one two three

$ echo one\ntwo\nthree | xargs -L1 echo "pre"
pre one
pre two
pre three

read

Read input from user

$ echo "$(read -p 'Tell me something? ' something; echo $something)"
Tell me something? hello
hello

exit

Exits with error when non-zero

$ $(exit 0) || echo hello
$ $(exit 1) || echo hello
hello

curl

Get headers

curl -I

Follow redirects

curl -L

Clone this wiki locally