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}"


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

or let cat collect it for you:

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