Some notes and examples of bash commands and common command line utilities I frequently use from bash.
A lot of the examples here are similar to stackoverflow threads, but here I get to record my favorite solutions and demo why I like them and give some extra context?
I asked GhatGPT "What are common PS1 settings to help with bash programming?" and after a bit oof trial and error, I added these
$ tail -n 4 ~/.bash_profile
PS1="\w \$(parse_git_branch) \$ "
parse_git_branch() {
git branch 2>/dev/null | grep '^*' | colrm 1 2
}
and now my prompt is
~/projects/bash-by-example master $ echo "hello PS1 world:" $PS1
hello PS1 world: \w $(parse_git_branch) $
Add iterm2 via brew:
brew install --cask iterm2
Add the following shortcuts
alias h='history'
alias h40='history|tail -40'
alias hl='history|less'
alias l40='ls -ltr|tail -40'
alias lt='ls -ltr|tail -10'
alias ht='history|tail -10'
Set bash to the default shell
# https://superuser.com/questions/48226/how-do-i-set-my-shell-in-mac-os-x
chsh -s /bin/bash
- Download anacondo
https://www.anaconda.com/products/distribution
- Setup pandas_playground
conda info --envs
conda activate pandas_playground
conda install pandas jupyter bottleneck numexpr matplotlib
conda update --all
- Final from terminal
jupyter notebook
- psql -E or set ECHO_HIDDEN to pg_catalog metadata - VERY USEFUL - details here
I've been struggling with running bash loops with input parameters of the format {1..$1}
... this never quite works for me (I've tried a few iterations).
The solution appears to be to use a while loop
[root@c7-master vagrant]# cat loop.sh
#echo $1
i=1
while [ "$i" -le "$1" ]; do
uptime
sleep 2
i=$((i+1))
#echo $i
done
e.g.
[root@c7-master vagrant]# bash loop.sh 3
21:26:02 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
21:26:04 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
21:26:06 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
[root@c7-master vagrant]# bash loop.sh 4
21:26:10 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
21:26:12 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
21:26:14 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
21:26:16 up 19 days, 50 min, 2 users, load average: 0.00, 0.01, 0.05
[root@c7-master vagrant]#
I thought which
was part of the standard bash shell, apparently not
-bash-4.2$ which psql
-bash: which: command not found
-bash-4.2$ exit
logout
-bash-4.2# yum install -y which
Loaded plugins: ovl
Resolving Dependencies
--> Running transaction check
---> Package which.x86_64 0:2.20-7.el7 will be installed
--> Finished Dependency Resolution
I suspect which
is standard for rhel and ubuntu, but maybe not in more minimal docker linux images?
Starting with projects
directory
[~] # ls -l | grep proj
drwxrwxr-x 26 dpitts 4096 feb 13 23:26 projects/
create an alias scripts
[~] # ln -s projects scripts
[~] # ls -l | grep proj
drwxrwxr-x 26 dpitts 4096 feb 13 23:26 projects/
lrwxrwxrwx 1 dpitts 8 feb 18 23:01 scripts -> projects/
While git does not support write-only access this is common elsewhere
Using awk to calculate sum and stddev
find with xargs and grep -H for deep search
awk escape characters, single quote and new_lines
- While I've routinely worked with bash directly on the command-line (on a daily basis) for almost the last two decades (in the early days I was using ksh and csh), more recently I've started using Visual Studio Code and a debugger extension
- https://github.com/rogalmic/vscode-bash-debug
- https://code.visualstudio.com/docs/editor/debugging
- There are no new scripts here, but some code samples and I do cover different bash versions, in the context of macosx and using brew (the package manager for macosx) to install a second and newer version of bash
- generate 30000 random numbers before 0 and 9999
- use awk to sort list and extract the 95th percentile, which should be 'around 9500' +/-100 (i.e. between 9400 and 9600))
- if-then-elif... blocks are suprisingly fiddly in bash scripts so watch out for syntax
- ref: https://askubuntu.com/questions/444082/how-to-check-if-1-and-2-are-null
- ref: https://www.thegeekstuff.com/2010/06/bash-if-statement-examples/