#!/bin/bash
if [[ -p /dev/stdin ]]
then
PIPE=$(cat -)
echo "PIPE=$PIPE"
fi
echo "ARGS=$@"
curl ... | jq -r '.data'
python
``` curl ... | python3 -c "import sys, json; print(json.load(sys.stdin)['data'])" ```echo $((5 - 3))
declare -a List=(en it de es pt fr et fi)
for lang in "${List[@]}"; do
echo "$lang"
done
while read LINE; do
echo "$LINE"
done < ./file
trap "exit" INT
while...
| Operator | Description |
|---|---|
| [ ! EXPRESSION ] | The EXPRESSION is false. |
| [ -n STRING ] | The length of STRING is greater than zero. |
| [ -z STRING ] | The lengh of STRING is zero (ie it is empty). |
| [ STRING1 = STRING2 ] | STRING1 is equal to STRING2 |
| [ STRING1 != STRING2 ] | STRING1 is not equal to STRING2 |
| [ INTEGER1 -eq INTEGER2 ] | INTEGER1 is numerically equal to INTEGER2 |
| [ INTEGER1 -gt INTEGER2 ] | INTEGER1 is numerically greater than INTEGER2 |
| [ INTEGER1 -lt INTEGER2 ] | INTEGER1 is numerically less than INTEGER2 |
| [ INTEGER1 -le INTEGER2 ] | INTEGER1 is numerically less than of equal to INTEGER2 |
| [ -d FILE ] | FILE exists and is a directory. |
| [ -e FILE ] | FILE exists. |
| [ -r FILE ] | FILE exists and the read permission is granted. |
| [ -s FILE ] | FILE exists and it's size is greater than zero (ie. it is not empty). |
| [ -w FILE ] | FILE exists and the write permission is granted. |
| [ -x FILE ] | FILE exists and the execute permission is granted. |
| [[ "" =~ REGEX ]] | REGEX check [[ "$foo" =~ ^bar$ ]] |
| if [ ... ] && [ ... ] | AND |
| if [ ... ] || [ ... ] | OR |
// you always need to remember to double quote variables https://stackoverflow.com/questions/13617843/unary-operator-expected-error-in-bash-if-condition
if [ "$lang" = "en" ] ; then
# ...
elif [ "$lang" = "fr" ] ; then
# ...
else
# ...
fi
for f in *.txt; do echo "mv $f ${f%%.txt}.md"; done
One line If
if [ $? -eq 0 ]; then echo 1; else echo 0; fi
Run command only after previous command is successful
command1 && command2
Run command only after previous command is unsuccessful
command1 || command2
File masks
ls file.(js|ts)
mv file.{js,ts} # mv file.js file.ts
echo -ne "Processing $i...\033[0K\r";
throw() {
echo -ne "\nERROR: $1\n"
return 1
}
warn() {
echo -ne "\n\033[1;33mWARN: $1\033[0m\n"
return 0
}
printf '\e]8;;https://example.com\e\\example\e]8;;\e\\'
echo -e '\e]8;;https://example.com\e\\example\e]8;;\e\\'
echo '\a'
__dirname=`dirname $0`
__filename=`basename $0`
# This is usual comment
echo a `# inline comment` \
b # comment at the end of command
load() {
($1) & BACK_PID=$!
spinner=(π π π π π π π π π π π π);
while kill -0 $BACK_PID &> /dev/null ; do
for c in "${spinner[@]}"; do echo -en "\033[2K\033[0;34m\r$c\033[0m $2"; sleep 0.1; done;
done
}
x() { find . &>/dev/null; }
load x "In progress..."
progress() {
($1) & BACK_PID=$!
c=".";
while kill -0 $BACK_PID &> /dev/null ; do
l=$(printf "%-$(</tmp/progress)s" "$c");
echo -en "$(tput cuu1)\033[2K\r$2\n\033[2K\r\033[0;34m${l// /$c}\033[0m"; sleep 0.1;
done
echo -en "$(tput cuu1)\033[2K\r"
}
x() {
echo 0 > /tmp/progress
sleep 1;
echo 50 > /tmp/progress
sleep 1;
echo 100 > /tmp/progress
}
progress x "In progress..."
echo -en "\033[2K\r\033[0;32mDone β\033[0m\n"
cd "${0%/*}"