{"id":22088246,"url":"https://github.com/hchiam/learning-bash-scripts","last_synced_at":"2025-03-23T22:41:38.725Z","repository":{"id":41161405,"uuid":"214250618","full_name":"hchiam/learning-bash-scripts","owner":"hchiam","description":"Learning Bash Scripts","archived":false,"fork":false,"pushed_at":"2025-02-03T05:22:26.000Z","size":154,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T08:03:41.879Z","etag":null,"topics":["bash","shell"],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hchiam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2019-10-10T17:58:16.000Z","updated_at":"2025-02-03T05:22:30.000Z","dependencies_parsed_at":"2023-11-13T03:27:07.848Z","dependency_job_id":"b2df217d-9094-4bba-a2ee-79d5425a7204","html_url":"https://github.com/hchiam/learning-bash-scripts","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/hchiam%2Flearning-bash-scripts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hchiam%2Flearning-bash-scripts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hchiam%2Flearning-bash-scripts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hchiam%2Flearning-bash-scripts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hchiam","download_url":"https://codeload.github.com/hchiam/learning-bash-scripts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245181527,"owners_count":20573717,"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":["bash","shell"],"created_at":"2024-12-01T02:07:55.301Z","updated_at":"2025-03-23T22:41:38.717Z","avatar_url":"https://github.com/hchiam.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learning Bash Scripts\n\nJust one of the things I'm learning. \u003chttps://github.com/hchiam/learning\u003e\n\n[You type commands in a terminal or console, and those commands get translated by the shell for the kernel to perform.](https://www.quora.com/What-is-the-difference-among-shell-console-terminal-and-kernel-Also-what-is-the-difference-between-shell-and-application-programs)\n\nA nice article on key things to know: https://www.joshwcomeau.com/javascript/terminal-for-js-devs (my own summary notes are below)\n\nTo enable running a script upon click, you need to edit permissions. For example, for `chrome-incognito-shortcut.app`:\n\n```bash\nchmod 744 chrome-incognito-shortcut.app\n```\n\n## Related Repos\n\n\u003chttps://github.com/hchiam/learning-powershell\u003e\n\n\u003chttps://github.com/hchiam/learning-dependency-cruiser\u003e\n\n\u003chttps://github.com/hchiam/learning-https-server\u003e\n\n## `\u003e` and `\u003c` and `\u003e\u003e` and `\u003c\u003c` in bash\n\nhttps://superuser.com/questions/480599/with-regards-to-piping-commands-what-are-the-greater-than-and-less-than\n\n- `\u003e` = redirect output = \"(over)write to\", e.g.: `echo 'hello' \u003e file.txt`\n- `\u003c` = redirect input = \"read\", e.g.: `cat \u003c file.txt` (same as just `cat file.txt`)\n- `\u003e\u003e` = append to end of file, e.g.: `echo 'new last line of text' \u003e\u003e file.txt`\n- `cat \u003c\u003c EOF \u003e\u003e file.txt` = \"start multiline input session that ends when you type `EOF` and hit Enter\" (but personally, `nano` seems to do this simpler)\n\n## Useful `git` commands\n\n- `git commit --amend`\n\n- `git reset HEAD~1` to \"un-commit\"\n\n- `git pull origin dev` (if you want to merge commits into `dev` branch into your local branch) and then type `:qa` and hit enter. Now you can `git push`.\n\n- `git tag \u003cname-of-tag\u003e; git push --tags` (and then you can draft a new release on the same tag name to add details to it on GitHub)\n\nFrom \u003chttps://stackoverflow.com/questions/4348590/how-can-i-make-git-ignore-future-revisions-to-a-file/39776107#39776107\u003e :\n\n- `git update-index --skip-worktree your-file-to-ignore-future-revisions-for.txt`\n- `git update-index --no-skip-worktree your-file-to-start-watching-revisions-for.txt`\n\nFrom \u003chttps://www.smashingmagazine.com/make-life-easier-when-using-git/\u003e :\n\n- `git status -sb`\n- git hooks\n- Try using git `bisect` with \"binary search\" automation to find the breaking change\n\n```bash\ngit bisect start\ngit bisect good c5ba734 # if c5ba734 is a commit without the bug\ngit bisect bad 6c093f4 #if 6c093f4 is a commit with the bug\n# run test: git bisect run ./test-bug  (or: git bisect run jest)\ngit bisect bad # if the current commit has the bug\ngit bisect good # if current commit does not have the bug\n# (repeats until find the first commit with the bug)\ngit bisect reset # or: git bisect reset HEAD (or: git bisect reset \u003ccommit-id\u003e)\n```\n\n```bash\ngit rebase -i --exec \"yarn test\" d294ae9 # test all commits from d294ae9 to HEAD, until hit first failing commit\n```\n\nFrom \u003chttps://dev.to/g_abud/advanced-git-reference-1o9j#git-commands\u003e :\n\n- `git rm --cached \u003cfile-name\u003e` to stop tracking `\u003cfile-name\u003e` in version control\n- `git diff --staged`\n- `git diff branch1..branch2`\n- `git commit --amend` to add more changes to the last commit\n- `git cherry-pick \u003ccommit-sha\u003e` to bring in a commit from a different branch\n- `git checkout \u003cbranch-name\u003e \u003cfile-name\u003e` to bring in a file from a differnt branch\n\n- `git reset \u003ccommit-sha\u003e`\n- `git reset --hard HEAD~1` = undo last commit and rewrite history (then `git push -f` to push rewritten history)\n- `git reset --hard HEAD~n # where n is the last n commits` = undo last n commit and rewrite history (then `git push -f` to push rewritten history)\n- `git reset --hard \u003ccommit-sha\u003e` = undo comments and rewrite history (then `git push -f` to push rewritten history)\n- `--soft` = uncommit but still staged; `--mixed` = uncommit+unstage, but changes kept locally; `--hard` = uncommit+unstage+delete changes\n\n- `git stash` to save your changes to top of stash stack (add `-u` to stash untracked files as well)\n- `git stash list` to see what's in your stash\n- `git stash save \"message to go along with changes\"`\n- `git stash pop`\n\nFrom \u003chttps://www.youtube.com/watch?v=ecK3EnyGD8o\u003e :\n\n- `git stash save nameforlater`\n\n  - and then later `git stash list` (note the index)\n  - and then `git stash apply \u003cindex\u003e` to use `\u003cindex\u003e`\n\n- `git branch -M main` to rename branch to main, and then `git push -u origin main` ([more info](https://www.git-tower.com/learn/git/faq/git-rename-master-to-main/))\n\n- `git log --graph --oneline --decorate` for a pared-down git log\n\n- lots more git tips at a [Fireship.io YouTube video](https://www.youtube.com/watch?v=ecK3EnyGD8o)\n\n### How to revert changes of a git push:\n\nThis shows the commit hash: (looks like `l0ngStr1ng0fL3t7er5AndNum83rz`)\n\n```bash\ngit log\n```\n\nthen use the hash of the commit you want to revert: (and when you're in the in-CLI editor, type `:qa` and then hit Enter)\n\n```bash\ngit revert l0ngStr1ng0fL3t7er5AndNum83rz\n```\n\n### How to create a branch with no history\n\nhttps://stackoverflow.com/a/34954852\n\n```bash\ngit checkout --orphan \u003cname_you_choose_for_orphan_branch\u003e\ngit commit\ngit push \u003cremote-name\u003e \u003cbranch-name\u003e\n```\n\n## Random Notes\n\nGet file info like edit date: `stat filename`\n\nExample of script used by Travis CI `npm run build` to copy files to `/public` folder: [copy-to-public-folder.sh](https://github.com/hchiam/hchiam.github.io/blob/master/copy-to-public-folder.sh)\n\nExample of script used to generate and insert a package SemVer number for a JS file: [package.sh](https://github.com/hchiam/_2DNote/blob/master/package.sh)\n\nExit/quit:\n\n```bash\nexit\n```\n\n### Get list of globally installed packages\n\n`yarn global list`\n\n`npm list -g --depth 0` for just top-level\n\n`npm list -g browser-sync` or a specific package (this example checks if `browser-sync` is installed globally)\n\n### npm scripts that accept parameters\n\nExamples I set up in package.json:\n\n```json\n{\n  \"test\": \"tsc $npm_config_name.ts --lib es6,dom \u0026\u0026 node $npm_config_name.js\",\n  \"demo\": \"echo \\\"HELLO $npm_config_file_name.ts GOODBYE (\\\\$npm_config_whatever will match --whatever=...)\\\"\"\n}\n```\n\nSo you can do this:\n\n```bash\nnpm run demo --file_name=someFileName\n# prints out: HELLO someFileName.ts GOODBYE [...]\n```\n\nand:\n\n```bash\nnpm run test --name=sameFileName\n# will run: tsc sameFileName.ts --lib es6,dom \u0026\u0026 node sameFileName.js\n```\n\n### Install package without dynamic version (i.e. without caret ^)\n\n```sh\nnpm install \u003cpackage-name\u003e --save-exact\n```\n\nso you get `1.2.3` instead of `^1.2.3` in your `package.json`\n\nhttps://stackoverflow.com/questions/58638817/what-is-the-purpose-of-using-save-exact#:~:text=--save-exact%20will%20generate%20the%20next%20package.json%20code\n\n### Actually upgrade node\n\nYou can upgrade to the latest `npm` with `npm install -g npm@latest`\n\nBut installing node manually doesn't always work, and using `n` or clean cache didn't seem to work for me. \u003chttps://stackoverflow.com/questions/23940172/not-seeing-latest-version-when-updating-node-js-via-installer-msi-windows-7/31229369#31229369\u003e\n\nFor Mac/OSX, try this first:\n\n```bash\nbrew install node\n```\n\nOtherwise this:\n\n```bash\nnvm install node --lts\nnode -v\n```\n\nIf you accidentally installed the latest but want to downgrade to the last stable instead:\n\n```bash\nnvm install 14.15.0 # whatever the latest unstable versioni on https://nodejs.org/en/\nnvm use 14.15.0\n```\n\n### switching from `bash` to `zsh`: (and back)\n\n\u003chttps://github.com/hchiam/learning-zsh\u003e\n\n### Format whole directory instead of waiting for each file save in VSCode\n\n```bash\nprettier --write .\n```\n\nor\n\n```bash\nnpx prettier --write .\n```\n\n### how to sync forked repo with the original repo\n\n\u003chttps://stackoverflow.com/questions/7244321/how-do-i-update-or-sync-a-forked-repository-on-github\u003e\n\n```bash\ngit remote add upstream https://github.com/\u003cowner\u003e/\u003crepo\u003e.git\ngit fetch upstream\n```\n\n### `gh` CLI commands\n\n\u003chttps://github.com/hchiam/learning-gh\u003e\n\n### setting up GitHub PATs (Personal Access Tokens)\n\nFor `gh` CLI: copy your PAT, run `gh auth login`, and paste your PAT when prompted to.\n\nFor npm pkg: copy your PAT, open `~/.npmrc`, and paste your PAT to be the \"TOKEN\" part of a string `//npm.pkg.github.com/:_authToken=TOKEN` in that `~/.npmrc` file.\n\n### quickly set up a basic server to serve index.html (or whatever's in the current folder)\n\n```sh\npython3 -m http.server 8000\n```\n\nor set up shortcut `srv` in .bash_profile:\n\n```\nalias srv='python3 -m http.server 8000'\n```\n\n## https://www.joshwcomeau.com/javascript/terminal-for-js-devs\n\n- Bash ≈ Zsh\n- the terminal is kinda like the console log in browser dev tools, but can switch between shell languages\n- `echo` ≈ `console.log`\n- `$` notation is just for start of prompt\n- `pwd` = print the current working directory folder path\n- `ls` = list current folder files/subfolders\n  - `ls -la` = `-l`ong data too like updated dates, and `-a`ll files including hidden files/folders\n- `cd` = change directory\n- `~` = home folder, like `/Users/yourusernamehere`\n- `.` = current folder\n- `..` = parent folder\n- use the tab key to auto-complete!\n- `rm` = remove\n  - `rm -r foldername` = remove `foldername` and `-r`ecursively inside it too\n  - `rm -rf foldername` = do that, but `-f`orce remove regardless of permissions\n- `man somecommand` = show a printout of the `man`ual for somecommand\n  - hit `q` to `q`uit (arrow keys to scroll)\n- `ping 8.8.8.8` = continuously check latency against IP address 8.8.8.8 (Google's DNS server), which can be used to continuously check if the server is online (`Ctrl`+`c` or `Ctrl`+`d` to stop)\n- `-` = \"previous\"\n  - `cd -` = toggle to previous folder\n  - `git checkout -` = toggle to previously-checked-out branch\n- `alias hi='echo \"Hello world!\"'`\n  - now you can just do `hi` to get a printout of `Hello world!`\n- `command1 \u0026\u0026 command2` = do command1 and then to command2\n- stuck in Vi/Vim? `Ctrl`+`c` won't work! hit `Escape` -\u003e `:` -\u003e `q!` -\u003e `Enter`\n- common web dev tasks: https://www.joshwcomeau.com/javascript/terminal-for-js-devs/#common-development-tasks\n  - reinstall dependencies: `rm -rf node_modules; npm install;`\n\n## more to learn for unix:\n\n- https://www.tutorialspoint.com/unix/unix-special-variables.htm\n- https://www.tutorialspoint.com/unix/unix-using-arrays.htm\n- https://www.tutorialspoint.com/unix/unix-basic-operators.htm\n- https://www.tutorialspoint.com/unix/unix-decision-making.htm\n- https://www.tutorialspoint.com/unix/unix-shell-loops.htm\n- https://www.tutorialspoint.com/unix/unix-quoting-mechanisms.htm for getting variables and special characters parsed or not parsed in strings\n- https://www.tutorialspoint.com/unix/unix-io-redirections.htm - there's more to it than just \u003e file\n- https://www.tutorialspoint.com/unix/unix-shell-functions.htm\n- https://www.tutorialspoint.com/unix/unix-manpage-help.htm\n- https://www.tutorialspoint.com/unix/unix-useful-commands.htm\n\n## 5 linux command tricks from fireship.io\n\nhttps://www.youtube.com/shorts/fwBIZRq-vzY\n\n- `mkdir`, but with multiple sibling folders at the same time:\n  - `mkdir folder` then:\n    ```sh\n    mkdir -p folder/{sibling1,sibling2}/{commonA,b,c}\n    ```\n  - gives you this:\n    ```txt\n    /folder\n      /sibling1\n        /commonA\n        /b\n        /c\n      /sibling2\n        /commonA\n        /b\n        /c\n    ```\n- `cd`, but back to where you last were instead of `cd ..` a bunch of times:\n  - `cd ./somewhere/deeply/nested` then to get back to `./`:\n    ```sh\n    cd -\n    ```\n- `touch`, but create a bunch of files with names with a pattern:\n  - ```sh\n    touch file{1..10}.txt\n    ```\n    =\n    ```txt\n    file1.txt\n    file2.txt\n    file3.txt\n    file4.txt\n    file5.txt\n    file6.txt\n    file7.txt\n    file8.txt\n    file9.txt\n    file10.txt\n    ```\n  - (note: the one-liner command doesn't pad with 0s on my machine despite `{01..10}`)\n  - two 0-padding alternatives that worked for me: (`%02g` = zero-padding of width 2 of general number format)\n    - ```sh\n      touch $(seq -f \"file%02g.txt\" 1 10)\n      ```\n    - or this: (the space after `txt ` matters for this one!)\n      ```sh\n      touch $(printf \"file%02g.txt \" {1..10})`\n      ```\n- `tail err.log` to read a file, but `tail -f err.log` to \"follow\" changes\n  - (note: doesn't seem to print out the right contents if i edit in another tab with `nano`)\n- `history`, but to get the last 5 commands:\n  - ```sh\n    history 5\n    ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhchiam%2Flearning-bash-scripts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhchiam%2Flearning-bash-scripts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhchiam%2Flearning-bash-scripts/lists"}