<required_argument>
[optional_argument]
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
echo $(realpath $0)
echo $(cd ${0%/*} && pwd -P)
echo ${0##*.}
my_function() {
if [ $# -lt 2 ]
then
echo "Usage: $funcstack[1] <first-argument> <second-argument>"
return
fi
echo "First argument: $1"
echo "Second argument: $2"
}
Usage
$ my_function
Usage: my_function <first-argument> <second-argument>
$ my_function foo
Usage: my_function <first-argument> <second-argument>
$ my_function foo bar
First argument: foo
Second argument: bar
exec -l $SHELL
alias <alias-name>
date +%Y-%m-%d_%H-%M-%S
s=abc
echo ${s#?} # bc
echo ${s%?} # ab
directory=~/foo
[ -e ${directory} ] || mkdir -p ${directory}
echo hello > /dev/null # redirects stdout only.
echo hello 2> /dev/null # redirects stderr only.
echo hello &> /dev/null # redirects both stdout and stderr.
S=old_and_old
echo ${S/old} # deletes the first "old" and shows "_and_old".
echo ${S//old} # deletes all the "old" and shows "_and_".
echo ${S/old/new} # replace the first "old" and shows "new_and_old".
echo ${S//old/new} # replace all the "old" and shows "new_and_new".
cat << EOF > sample.txt
aa
bb
cc
EOF
realpath <file-or-folder>
file -b sample.txt
s=sample.txt
echo Basename: $s:r # sample
echo Extension: $s:e # txt
<command> &|
https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html
rsync -a src dst # Note that it's not "/src" but "src"
# Note "src/", not "src"
rsync -a src dst
$ cat input.txt
aa
bb
cc
$ paste -s -d, input.txt
aa,bb,cc
$ cat left.txt
aa
bb
$ cat right.txt
xx
yy
$ paste -d, left.txt right.txt
aa,xx
bb,yy
$ paste -d'\0' left.txt right.txt
aaxx
bbyy
-t
is a separator-1
is the one-based index of a matching column from a first input file.-2
is the one-based index of a matching column from a second input file.
$ cat foo.txt
1,aa
2,bb
4,cc
$ cat bar.txt
1,xx
2,yy
5,zz
$ join -t, -1 1 -2 2 foo.txt bar.txt
1,aa,xx
2,bb,yy
#kill found processes
ps aux | grep "my_process_name" | awk '{ print $2 }' | xargs kill
#check if command exists
command -v foo > /dev/null 2>&1 || echo 'command not found.'
#one liner
true && { echo 'true1'; echo 'true2'; } || { echo 'false1'; echo 'false2'; }
false && { echo 'true1'; echo 'true2'; } || { echo 'false1'; echo 'false2'; }
# tee both stdout and stderr
./foo.sh 2>&1 | tee -a foo.log
# set JAVA_HOME if not set
[ -z ${JAVA_HOME} ] && export JAVA_HOME=/path/to/javahome
#use DEFAULT_JAVA_HOME if JAVA_HOME not set
DEFAULT_JAVA_HOME=/path/to/javahome
${JAVA_HOME=${DEFAULT_JAVA_HOME}} yourclassfile
#get timstamp in yyyyymmdd
stat -c %y ${FILE1} | awk '{print $1}' | tr -d '-'
#Extract file from targz
tar fvxz foo.tar.gz -C /path/to/destination file_in_targz > /dev/null