Skip to content

Latest commit

 

History

History
74 lines (63 loc) · 987 Bytes

shell.md

File metadata and controls

74 lines (63 loc) · 987 Bytes

Brace expansion

for file in ~/{file1,file2}; do
	if [[ -r "$file" ]] && [[ -f "$file" ]]; then
		source "$file"
	fi
done

Loops

## for loop:
for (( i = 0; i < 20; i++ )); do
  echo $i
done
## While loop:
i=0
while [[ $i -lt 20 ]]; do
  echo $i
  let i++
done
## For loop using seq:
for i in $(seq 0 19); do
  echo $i
done
## brace expansion
for i in {0..19}; do
  echo $i
done

if else

$? = 0 = true $? > 0 = false

arrays

+= operator is available

ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')

confirmation

# Here I have to modify IFS to capt \n and use -N and not -n
# See Escape sequence note
IFS= read -r -N 1 -p $'Continue \e[31m(y/n)?\e[m ' choice
if [ ! "$choice" == $'\n' ]; then echo; fi
case "$choice" in
  [yY])
		echo "yes"
		;;
  [nN]|$'\n')
		echo "no"
		;;
  *)
		echo "invalid"
		;;
esac

print special characters, ex newline

printf 'here %q\n' "$IFS"

date

$> date '+%d.%m.%y-%H.%M.%S’