{"id":25629665,"url":"https://github.com/SimonBaeumer/shell-tricks","last_synced_at":"2026-06-10T13:30:21.120Z","repository":{"id":56411796,"uuid":"170160117","full_name":"SimonBaeumer/shell-tricks","owner":"SimonBaeumer","description":"Simple bash tricks which make your life easier.","archived":false,"fork":false,"pushed_at":"2022-07-21T19:24:32.000Z","size":23,"stargazers_count":168,"open_issues_count":1,"forks_count":9,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-11T08:51:21.114Z","etag":null,"topics":["bash","linux","posix","sh","shell","tricks"],"latest_commit_sha":null,"homepage":"","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/SimonBaeumer.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}},"created_at":"2019-02-11T16:16:08.000Z","updated_at":"2025-02-07T18:31:19.000Z","dependencies_parsed_at":"2022-08-15T18:10:49.010Z","dependency_job_id":null,"html_url":"https://github.com/SimonBaeumer/shell-tricks","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/SimonBaeumer%2Fshell-tricks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonBaeumer%2Fshell-tricks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonBaeumer%2Fshell-tricks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonBaeumer%2Fshell-tricks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SimonBaeumer","download_url":"https://codeload.github.com/SimonBaeumer/shell-tricks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240222484,"owners_count":19767465,"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","linux","posix","sh","shell","tricks"],"created_at":"2025-02-22T19:17:33.978Z","updated_at":"2026-06-10T13:30:16.961Z","avatar_url":"https://github.com/SimonBaeumer.png","language":"Shell","funding_links":[],"categories":["Shell"],"sub_categories":[],"readme":"# Shell tricks\n\n- [Switch to previous directory](#switch-to-previous-directory)\n  * [git](#git)\n  * [cd](#cd)\n- [Get global ip](#get-global-ip)\n- [Simple commands](#simple-commands)\n- [Loop](#loop)\n- [Loop with specified increment each iteration](#loop-with-specified-increment-each-iteration)\n- [Sequences of letters or numbers](#sequences-of-letters-or-numbers)\n- [Reuse arguments](#reuse-arguments)\n- [Reuse commands](#reuse-commands)\n- [Compare output of two commands](#compare-output-of-two-commands)\n- [Fix last command](#fix-last-command)\n- [Accept interactive commands](#accept-interactive-commands)\n- [Last exit code](#last-exit-code)\n- [Easy backup](#easy-backup)\n- [Print to stderr](#print-to-stderr)\n- [Debugging](#debugging)\n- [Useful readline tricks](#useful-readline-tricks)\n- [Repeat command](#repeat-command)\n- [Substrings](#substrings)\n- [More resources](#more-resources)\n  \n## Switch to previous directory\n\nSwitch between the current and previous branch / directory. \n\n### git\n\n```bash\n$ git branch\n* master\ndevelopment\n\n$ git checkout development\nSwitched to branch 'development'\n$ git checkout - # Switch to previous\nSwitched to branch 'master'\n$ git checkout -\nSwitched to branch 'development'\n```\n\n### cd\n\n```bash\n$ pwd\n/\n$ cd /tmp\n$ cd - # Switch to previous\n/\n$ cd -\n/tmp\n```\n\n## Get global ip\n\n```bash\n$ curl ifconfig.co # IPv4\n50.110.14.21\n$ curl -6 ifconfig.co # IPv6\n2010:3f3f:113f:0:ea57:4497:7291:e422\n```\n\n## Simple commands\n\nCreate a script which calls functions by its` first argument. This is very useful to create simple scripts which could be a wrapper for other commands.\n\n```bash\n#!/usr/bin/env bash\n\nfunction do_this () { echo \"call do_this function\"; }\n\nfunction do_sth() { echo \"call do_sth function\" }\n\ncase \"$1\" in\n    do_this|do_sth) \"$1\" ;;\nesac\n```\n\nExecute it:\n\n```bash\n$ ./simple-commands.sh do_this\ncall do_this function\n```\n\n## Loop\n\nWrite simple one liner loops if you need to do some batch tasks.\n\n```bash\n$ for i in {1..10}; do echo \"$i\"; done\n\n# List disk usage by directories\n$ for file in */ .*/ ; do du -sh $file; done\n```\n\n## Loop with specified increment each iteration\n\n```bash\nfor i in {1..100..2}; do echo $i; done\n1\n3\n5\n7\n...\n```\n\n## Sequences of letters or numbers\n\nBrace expansion is great for lots of things.\n\n``` bash\n$ touch file{a..c}\n$ ls\n$ command ls\nfilea fileb filec\n```\n\n``` bash\n$ touch file-{1..15}\n$ ls\nfile-1\tfile-10\tfile-11\tfile-12\tfile-13\tfile-14\tfile-15\tfile-2\tfile-3\tfile-4\tfile-5\tfile-6\tfile-7\tfile-8\tfile-9\n$ ls file-{9..12}\nfile-10\tfile-11\tfile-12\tfile-9\n```\n\n``` bash\n$ printf \"%s\\n\" file-{a..c}{1..3}\nfile-a1\nfile-a2\nfile-a3\nfile-b1\nfile-b2\nfile-b3\nfile-c1\nfile-c2\nfile-c3\n```\n\n(If you give `printf` more arguments than it expects, it automatically loops.)\n\n## Reuse arguments\n\n```bash\n$ ls /tmp\nsome_file.txt some_archive.tar.gz\n$ cd !$\n/tmp\n```\n\n## Reuse commands\n\n```bash\n$ echo \"reuse me\"\nreuse me\n$ !!\necho \"reuse me\"\nreuse me\n```\n\n## Compare output of two commands\n```bash\ndiff \u003c(echo \"1 2 4\") \u003c(echo \"1 2 3 4\")\n1c1\n\u003c 1 2 4\n---\n \u003e 1 2 3 4\n```\n\n## Fix last command\n\n```bash\n$ ehco foo bar bar\nbash: ehco: command not found\n$ ^ehco^echo   \nfoo bar baz \n```\n\n## Accept interactive commands\n\n```bash\n$ yes | ./interactive-command.sh\nAre you sure (y/n)\nAccepted\nyes: standard output: Broken pipe\n```\n\nThe error message is printed because `yes` gets killed by `SIGPIPE` signal. This happens\nif the pipe to `./interactive-command.sh` gets closed but `yes` still wants to write into it.\n\nIgnore error message:\n\n`$ yes 2\u003e/dev/null | ./interactive-command.sh`\n\n## Last exit code\n\n```bash\n$ ls /tmp\nsome_file.txt\n$ echo $?\n0\n```\n\n## Easy backup\n\n```bash\n$ cp file.txt{,.bak}\n$ ls -1\nfile.txt\nfile.txt.bak\n```\n\n## Print to stderr\n\n```\n$ \u003e\u00262 echo hello\nhello\n```\n\n## Debugging\n\nAdd `-xv` to your bash scripts, i.e.:\n\n```\n/usr/bin/env bash\nset -xv\n```\n\nor `/bin/bash -xv script.sh`\n\n## Useful `readline` tricks\n\nIf you use the standard `bash` `readline` bindings.\n\n- `C-a` (aka `CTRL+A`) move cursor to beginning of line\n- `C-e` (aka `CTRL+E`) move cursor to end of line\n- `M-.` (aka `ALT+.`)  insert last argument of previous command (like `!$`, but you can edit it)\n\n## Repeat command\n\nExecute a command every two seconds and monitor its` output.\nThis is especially useful for waiting until a deployment or infrastructure provisioning is completed, i.e. on aws.\n\n`watch -n2 echo hello`\n`\n## Substrings\n\n```\n$ a=\"apple orange\"\n\n$ echo ${a#* }\norange\n$ echo ${a#*p}\nple orange\n$ echo ${a##*p}\nle orange\n\n$ echo ${a% *}\napple\n$ echo ${a%p*}\nap\n$ echo ${a%%p*}\na\n```\nThe # for finding first occurence from the start and % for the first occurence from the end. * for matching any pattern. For greedy matching ## and %%. \n\n## More resources\n\n - [BashFAQ](https://mywiki.wooledge.org/BashFAQ) - A full FAQ of useful stuff\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSimonBaeumer%2Fshell-tricks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSimonBaeumer%2Fshell-tricks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSimonBaeumer%2Fshell-tricks/lists"}