Generic shell scripting tricks

Some might be POSIX/portable, some not. I am too old for that crap.

<< Back to wiki homepage

Table of contents:

Get last argument given

echo "${@: -1}"

Stdin

Check if stdin is empty or not

if [ ! -t 0 ]; then
    # there is stuff in stdin

Catch and use stdin

2 ways:

while IFS= read -r line; do
    printf '%s\n' "$line" >> /whatever/you/want/to/do
done

or let cat collect it for you:

data=$(cat)
stuff="`printf '%s\n' "$data"`"