{"id":16445195,"url":"https://github.com/dgapitts/bash-by-example","last_synced_at":"2025-03-26T21:18:03.495Z","repository":{"id":145829194,"uuid":"260749008","full_name":"dgapitts/bash-by-example","owner":"dgapitts","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-25T12:59:45.000Z","size":1155,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-12T09:47:08.192Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgapitts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-02T18:24:37.000Z","updated_at":"2024-07-25T12:59:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"c2161465-4328-4140-9378-33ed21546a83","html_url":"https://github.com/dgapitts/bash-by-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgapitts%2Fbash-by-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgapitts%2Fbash-by-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgapitts%2Fbash-by-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgapitts%2Fbash-by-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgapitts","download_url":"https://codeload.github.com/dgapitts/bash-by-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245735886,"owners_count":20663807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T09:43:36.926Z","updated_at":"2025-03-26T21:18:03.477Z","avatar_url":"https://github.com/dgapitts.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bash-by-example\n## Introduction\n\nSome notes and examples of bash commands and common command line utilities I frequently use from bash.\n\nA 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?\n\n## example 19 - simple PS1 settings to help with bash shell\n\nI asked GhatGPT \"What are common PS1 settings to help with bash programming?\" and after a bit oof trial and error, I added these \n```\n$ tail -n 4 ~/.bash_profile\nPS1=\"\\w \\$(parse_git_branch) \\$ \"\nparse_git_branch() {\n    git branch 2\u003e/dev/null | grep '^*' | colrm 1 2\n}\n```\nand now my prompt is \n```\n~/projects/bash-by-example master $ echo \"hello PS1 world:\" $PS1\nhello PS1 world: \\w $(parse_git_branch) $\n```\n\n## example 18 - setting terminal on new macbook\n\nAdd iterm2 via brew:\n```\nbrew install --cask iterm2\n```\n\nAdd the following shortcuts\n```\nalias h='history'\nalias h40='history|tail -40'\nalias hl='history|less'\nalias l40='ls -ltr|tail -40'\nalias lt='ls -ltr|tail -10'\nalias ht='history|tail -10'\n```\nSet bash to the default shell\n```\n# https://superuser.com/questions/48226/how-do-i-set-my-shell-in-mac-os-x\nchsh -s /bin/bash\n```\n\n## example 17 - getting started with conda and jupyter notebook\n\n\n* Download anacondo\n\nhttps://www.anaconda.com/products/distribution\n\n* Setup pandas_playground\n\n```\nconda info --envs\nconda activate pandas_playground\nconda install pandas jupyter bottleneck numexpr matplotlib\nconda update --all\n\n```\n\n* Final from terminal \n\n```\njupyter notebook\n```\n\n\n\n\n\n## example 16 - psql - probably the best command line db editor\n\n* psql -E or set ECHO_HIDDEN to pg_catalog metadata - VERY USEFUL - [details here](docs/psql-E-or-ECHO_HIDDEN.md)\n\n## example 15 - bash loop with limit $1\n\n\nI'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).\n\nThe solution appears to be to use a while loop\n```\n[root@c7-master vagrant]# cat loop.sh \n#echo $1\ni=1\nwhile [ \"$i\" -le \"$1\" ]; do\n   uptime\n   sleep 2\n   i=$((i+1))\n   #echo $i\ndone\n```\ne.g.\n```\n[root@c7-master vagrant]# bash loop.sh 3\n 21:26:02 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n 21:26:04 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n 21:26:06 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n[root@c7-master vagrant]# bash loop.sh 4\n 21:26:10 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n 21:26:12 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n 21:26:14 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n 21:26:16 up 19 days, 50 min,  2 users,  load average: 0.00, 0.01, 0.05\n[root@c7-master vagrant]# \n```\n\n## example 014 - which: command not found\n\nI thought `which` was part of the standard bash shell, apparently not\n```\n-bash-4.2$ which psql\n-bash: which: command not found\n-bash-4.2$ exit                     \nlogout\n-bash-4.2# yum install -y which\nLoaded plugins: ovl\nResolving Dependencies\n--\u003e Running transaction check\n---\u003e Package which.x86_64 0:2.20-7.el7 will be installed\n--\u003e Finished Dependency Resolution\n```\n\nI suspect `which` is standard for rhel and ubuntu, but maybe not in more minimal docker linux images?\n\n\n## example 013 - adding an alias\n\n\nStarting with `projects` directory\n```\n[~] # ls -l | grep proj\ndrwxrwxr-x 26 dpitts     4096 feb 13 23:26 projects/\n```\n\ncreate an alias `scripts`\n```\n[~] # ln -s projects scripts\n[~] # ls -l | grep proj\ndrwxrwxr-x 26 dpitts     4096 feb 13 23:26 projects/\nlrwxrwxrwx  1 dpitts        8 feb 18 23:01 scripts -\u003e projects/\n```\n## example 012 - simple load testing via pgbench\n\n* [yum install postgresql](docs/yum-install-postgresql.md)\n* [postgresql13-contrib required for pgbench (plus bse usage notes)](docs/postgresql13-contrib_required-for-pgbench.md)\n\n## example_011 atop - yum install and config\n* [yum install atop via epel](docs/yum-install-atop-via-epel.md)\n* [atop extra config](docs/atop-extra-config.md)\n* [htop-intro](docs/htop-intro.md)\n\n\n## example_010 extra_alias_start_stop_postgres\n[Some custom extra alias](example_010_extra_alias_start_stop_postgres/README.md)\n\n\n## example_009 chmod_read_write_exec\n[While git does not support write-only access this is common elsewhere](example_009_chmod_read_write_exec/README.md)\n\n## example_008 bash_awk_sum_stddev\n[Using awk to calculate sum and stddev](example_008_bash_awk_sum_stddev/README.md)\n\n## example_007 find_xargs_grepH\n[find with xargs and grep -H for deep search](example_007_find_xargs_grepH/README.md)\n\n## example_006 introducing_ShellCheck (to be completed)\n[RHEL setup shellcheck](example_006_introducing_ShellCheck/README.md)\n\n## example_005 awk_special_characters\n[awk escape characters, single quote and new_lines](example_005_awk_special_characters/README.md)\n\n## example_004 getting_started_with_git\n[intro to git cli](example_004_getting_started_with_git/README.md)\n\n## example_003 vscode-bash-debug_setup_and_brew\n- 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\n- https://github.com/rogalmic/vscode-bash-debug\n- https://code.visualstudio.com/docs/editor/debugging\n- 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\n\n\n## example_002 bash_random_and_awk_95th_percentile\n- generate 30000 random numbers before 0 and 9999\n- use awk to sort list and extract the 95th percentile, which should be 'around 9500' +/-100 (i.e. between 9400 and 9600))\n\n## example_001 input_flags to scripts and if then elif fi blocks\n- if-then-elif... blocks are suprisingly fiddly in bash scripts so watch out for syntax\n- ref: https://askubuntu.com/questions/444082/how-to-check-if-1-and-2-are-null\n- ref: https://www.thegeekstuff.com/2010/06/bash-if-statement-examples/\n\n\n## Appendix - background references\n\n- ref: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#blockquotes\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgapitts%2Fbash-by-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgapitts%2Fbash-by-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgapitts%2Fbash-by-example/lists"}