Skip to content

Latest commit

 

History

History
91 lines (72 loc) · 1.42 KB

bash.md

File metadata and controls

91 lines (72 loc) · 1.42 KB

Loop through lines of a file

while read theLine || [[ -n $theLine ]]; do
	echo "$theLine"

done < "<THE_FILE_NAME>"

join function

join_by() {
	local IFS="$1"
	shift
	echo "$*"
}

# example
join_by , a "b c" d #a,b c,d

! commands

# re-run last command
!!

# Re-run command N commands ago
!-N

# Last parameter from previous command
!$

# All params from previous command
!*

Job Control

# list all jobs
jobs

# bring job to foreground
fg %N # where N is the job number obtained from jobs

# bring job to background
bg %N # where N is the job number obtained from jobs

Variables

# Get cli args but only from the 2nd element and onwards
"${@:2}"

# Prepend Foo to every element of an array, MY_ARRAY
${MY_ARRAY[@]/#/Foo}

# Append Foo to every element of an array, MY_ARRAY
${MY_ARRAY[@]/%/Foo}

# Convert a string, referenced by MY_VAR, to lowercase
"${MY_VAR,,}"

slurp file into variable

myVariable=$(<'<FILE_TO_SLURP>')

append string to files (before the extension)

# ${f%.*} - all chars before ext
# ${f##*.} - all chars after ext (dot not incl)
for f in *; do
	mv -v "$f" "${f%.*}<TEXT_TO_APPEND>.${f##*.}"
done

debug

bash -x "<COMMAND_TO_RUN>"

truncate file

> '<PATH_TO_FILE>'

Recursively move files to new directory

# recursively move all MOV files in cwd to a new dir
mv ./**/*.MOV '<TARGET_DIR>'